Laravel passport authentication issues with decoupled vuejs client - php

Hello fellow developpers, I need your wisdom
I am creating a laravel API with an oauth2 authentication process (passport authorization code flow) used by a Vuejs decoupled client and I can't authenticate through the usual process (401 Unauthenticated).
If I try with the laravel project blade views it works as expected, but as soon as I try to do a separated project for the frontend, I end up with a 401 Unauthenticated error when getting to the oauth/authorize route and I can't get any information about what's wrong so I can't go further in my project.
For now, I don't even use inputs, I just send the request I need :
(of course i send the parameters that suits the ones I have in the oauth client database)
public function authorizeTpp(Request $request)
{
$query = http_build_query([
'client_id' => '3',
'redirect_uri' => 'http://api.dirapp.test/auth/callback',
'response_type' => 'code',
'scope' => '*',
]);
return redirect('http://api.dirapp.test/oauth/authorize?'.$query);
}
Here is my AuthServiceProvider :
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
Here is my Kernel :
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
CreateFreshApiToken::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
And here are my routes :
web.php :
Auth::routes();
Route::get('auth/callback', 'Auth\LoginController#authCallback');
api.php :
Route::get('authorize', 'Auth\LoginController#authorizeTpp');
and the routes :
+--------+----------+-----------------------------------------+-----------------------------------+---------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-----------------------------------------+-----------------------------------+---------------------------------------------------------------------------+--------------+
| | GET|HEAD | api/authorize | | App\Http\Controllers\Auth\LoginController#authorizeTpp | api,guest |
| | POST | api/create-client | | App\Http\Controllers\HomeController#createOauthClient | api |
| | POST | api/create-user | | App\Http\Controllers\HomeController#createUser | api |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | auth/callback | | App\Http\Controllers\Auth\LoginController#authCallback | web,guest |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController#index | web |
| | POST | login | | App\Http\Controllers\Auth\LoginController#login | web,guest |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController#showLoginForm | web,guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController#logout | web |
| | GET|HEAD | oauth/authorize | passport.authorizations.authorize | Laravel\Passport\Http\Controllers\AuthorizationController#authorize | web,auth |
| | POST | oauth/authorize | passport.authorizations.approve | Laravel\Passport\Http\Controllers\ApproveAuthorizationController#approve | web,auth |
| | DELETE | oauth/authorize | passport.authorizations.deny | Laravel\Passport\Http\Controllers\DenyAuthorizationController#deny | web,auth |
| | POST | oauth/clients | passport.clients.store | Laravel\Passport\Http\Controllers\ClientController#store | web,auth |
| | GET|HEAD | oauth/clients | passport.clients.index | Laravel\Passport\Http\Controllers\ClientController#forUser | web,auth |
| | PUT | oauth/clients/{client_id} | passport.clients.update | Laravel\Passport\Http\Controllers\ClientController#update | web,auth |
| | DELETE | oauth/clients/{client_id} | passport.clients.destroy | Laravel\Passport\Http\Controllers\ClientController#destroy | web,auth |
| | GET|HEAD | oauth/personal-access-tokens | passport.personal.tokens.index | Laravel\Passport\Http\Controllers\PersonalAccessTokenController#forUser | web,auth |
| | POST | oauth/personal-access-tokens | passport.personal.tokens.store | Laravel\Passport\Http\Controllers\PersonalAccessTokenController#store | web,auth |
| | DELETE | oauth/personal-access-tokens/{token_id} | passport.personal.tokens.destroy | Laravel\Passport\Http\Controllers\PersonalAccessTokenController#destroy | web,auth |
| | GET|HEAD | oauth/scopes | passport.scopes.index | Laravel\Passport\Http\Controllers\ScopeController#all | web,auth |
| | POST | oauth/token | passport.token | Laravel\Passport\Http\Controllers\AccessTokenController#issueToken | throttle |
| | POST | oauth/token/refresh | passport.token.refresh | Laravel\Passport\Http\Controllers\TransientTokenController#refresh | web,auth |
| | GET|HEAD | oauth/tokens | passport.tokens.index | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController#forUser | web,auth |
| | DELETE | oauth/tokens/{token_id} | passport.tokens.destroy | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController#destroy | web,auth |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController#sendResetLinkEmail | web,guest |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController#showLinkRequestForm | web,guest |
| | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController#reset | web,guest |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController#showResetForm | web,guest |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController#showRegistrationForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\RegisterController#register | web,guest |
+--------+----------+-----------------------------------------+-----------------------------------+---------------------------------------------------------------------------+--------------+
When using internal laravel blade views, I can go through the whole process,
going to oauth/authorize route, getting the 'authorize' view where I can validate or cancel the client registration, then going to oauth/token through the callback route, and then getting my user authenticated and redirected to the homepage.
As soon as I try to get the same results with an external/decoupled frontend project (Vuejs), the oauth/authorize route gives me a 401 Unauthenticated error while using same credentials.
I don't know if it is related or not, but when using the decoupled client I don't have a session anymore and I loose the 'intended_url' usually used by the auth process to do the proper redirection and it brings me to the homepage instead.
To fix this, my API send to my client the proper encoded intended_url so I can send it back to the login function when needed and go to the real intended_url after the auth process.
I don't know what i'm doing wrong and I need help, I think I might have misunderstood the way passport works but there is not much informations about the authorization code flow process through a decoupled client that I can get.
I spent a few days on this without any success so if you have any information or can give me any help it would be awesome.
Thanks.

Related

Solve Laravel InvalidArgumentException: Authentication views not found

I updated laravel 5.6 project to laravel 6 in order to use the laravel-permission spatie package. It uses the default auth provided by Laravel. I can't access the auth routes and it appears as I am logged in while still seeing Login | Register on the navbar.
This is my route list
| GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController#index | web,auth |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController#showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\LoginController#login | web,guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController#logout | web |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController#sendResetLinkEmail | web,guest |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController#showLinkRequestForm | web,guest |
| | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController#reset | web,guest |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController#showResetForm | web,guest |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController#showRegistrationForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\RegisterController#register | web,guest |
| | GET|HEAD | {any} | | App\Http\Controllers\LexaController#index | web |
Your error suggests that Laravel can't find your auth views. Is your register and login view located at: resources/views/auth? You might have to move them there if they aren't.

Laravel Controller to Controller

I've built a website using Laravel that I'm happy with, however, due to not planning ahead I have built the admin/management panel, and now that I have to go through and do the user front end I'm wondering how I should have though to handle this.
Currently I have a web.php file containing lines like this:
# WEDDING HANDLING
Route::get('/admin/weddings', 'WeddingController#index');
Route::get('/admin/wedding/create', 'WeddingController#create');
Route::get('/admin/wedding/{wedding}', 'WeddingController#show');
Route::get('/admin/wedding/{wedding}/edit', 'WeddingController#edit');
Route::post('/admin/wedding/create', 'WeddingController#store');
Route::put('/admin/wedding/{wedding}/edit', 'WeddingController#update');
Route::delete('/admin/wedding/{wedding}/destroy', 'WeddingController#destroy');
# MENU HANDLING
Route::get('/admin/wedding/{wedding}/menus', 'MenuController#index');
Route::get('/admin/wedding/{wedding}/menu/create', 'MenuController#create');
Route::get('/admin/wedding/{wedding}/menu/{menu}', 'MenuController#show');
Route::get('/admin/wedding/{wedding}/menu/{menu}/edit', 'MenuController#edit');
Route::post('/admin/wedding/{wedding}/menu/create', 'MenuController#store');
Route::put('/admin/wedding/{wedding}/menu/{menu}/edit', 'MenuController#update');
Route::delete('/admin/wedding/{wedding}/menu/{menu}/delete', 'MenuController#destroy');
...continues
So from that you can probably see that I have multiple controllers that handle this for the admin, however, if a user logs in, they should be able to view the menus against the wedding, which I can stack with #auth tags, and seperate it out like that, however I'm afraid I'm going to end up with a web.php that looks like the following:
# WEDDING HANDLING
Route::get('/admin/weddings', 'WeddingController#index');
Route::get('/weddings', 'WeddingController#index');
...continues
Hopefully from my demo you can see what I'm asking.
tl;dr: How would you go about seperating User/Management areas without duplicating a ton of code.
You would need to create different controllers just to handle the front-end, like: FrontEndMenuController#yourFunction
you could also separate your admin routes into a group with the prefix 'admin' instead of repeating it on all the routes:
Route::prefix('admin')->group(function () {
Route::get('/wedding/{wedding}/menus', 'MenuController#index');
Route::get('/wedding/{wedding}/menu/create', 'MenuController#create');
Route::get('/wedding/{wedding}/menu/{menu}', 'MenuController#show');
Route::get('/wedding/{wedding}/menu/{menu}/edit', 'MenuController#edit');
});
And then have your front-end routes like that:
Route::get('/wedding/{wedding}/menu', 'FrontEndMenuController#index');
As you are almost adhering to the CRUD/REST convention, an addition to Adriano Marra's answer would be to also use resource controllers.
From Laravel documentation about resource controllers:
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code.
Introducing Resource Controllers
In your use case your resource are the weddings, so you could rewrite your web.php routes as:
Route::prefix('admin')->group(function () {
Route::resource('weddings', 'WeddingsController');
});
These lines would register such routes:
+--------+-----------+-------------------------------+------------------+-------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-------------------------------+------------------+-------------------------------------------------+------------+
| | GET|HEAD | admin/weddings | weddings.index | App\Http\Controllers\WeddingsController#index | web |
| | POST | admin/weddings | weddings.store | App\Http\Controllers\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/create | weddings.create | App\Http\Controllers\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding} | weddings.show | App\Http\Controllers\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding} | weddings.update | App\Http\Controllers\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding} | weddings.destroy | App\Http\Controllers\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/edit | weddings.edit | App\Http\Controllers\WeddingsController#edit | web |
+--------+-----------+-------------------------------+------------------+-------------------------------------------------+------------+
NOTE: There's a small difference as the Route::resource(...) method uses the plural resource name you provided to it (weddings) for all the seven routes, and the singular form for the parameter name (wedding).
You could then register the menus resource in the same way:
Route::prefix('admin')->group(function () {
Route::resource('weddings', 'WeddingsController');
// Personally, I would make 'menus' a top level resource but I will
// stick to your routing example for the rest of the answer.
Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});
Named Group Resource Routes
Furthermore you could also register the frontend wedding controller as a resource
Route::prefix('admin')->group(function () {
Route::resource('weddings', 'WeddingsController');
Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});
// This will map only two methods for this resource.
Route::resource('weddings', 'FrontendController')->only(['index', 'show']);
The routes registered by your application would be:
+--------+-----------+--------------------------------------------+------------------+-------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+--------------------------------------------+------------------+-------------------------------------------------+------------+
| | GET|HEAD | admin/weddings | weddings.index | App\Http\Controllers\WeddingsController#index | web |
| | POST | admin/weddings | weddings.store | App\Http\Controllers\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/create | weddings.create | App\Http\Controllers\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding} | weddings.show | App\Http\Controllers\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding} | weddings.update | App\Http\Controllers\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding} | weddings.destroy | App\Http\Controllers\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/edit | weddings.edit | App\Http\Controllers\WeddingsController#edit | web |
| | GET|HEAD | admin/weddings/{wedding}/menus | menus.index | App\Http\Controllers\WeddingsController#index | web |
| | POST | admin/weddings/{wedding}/menus | menus.store | App\Http\Controllers\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/create | menus.create | App\Http\Controllers\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu} | menus.show | App\Http\Controllers\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding}/menus/{menu} | menus.update | App\Http\Controllers\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding}/menus/{menu} | menus.destroy | App\Http\Controllers\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu}/edit | menus.edit | App\Http\Controllers\WeddingsController#edit | web |
| | GET|HEAD | weddings | weddings.index | App\Http\Controllers\FrontendController#index | web |
| | GET|HEAD | weddings/{wedding} | weddings.show | App\Http\Controllers\FrontendController#show | web |
+--------+-----------+--------------------------------------------+------------------+-------------------------------------------------+------------+
If you look close at the above table, you will notice that the name column has some duplicate values. This would create conflict when you need to reference these particular routes by name anywhere in your application.
So you could solve this by prefixing the admin group's named routes with a custom prefix:
Route::prefix('admin')->name('admin.')->group(function () {
Route::resource('weddings', 'WeddingsController');
Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});
// This will map only two methods for this resource.
Route::resource('weddings', 'FrontendController')->only(['index', 'show']);
This would solve any route conflict, as the administation routes has been correctly prefixed with admin. name:
+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------+------------+
| | GET|HEAD | admin/weddings | admin.weddings.index | App\Http\Controllers\WeddingsController#index | web |
| | POST | admin/weddings | admin.weddings.store | App\Http\Controllers\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/create | admin.weddings.create | App\Http\Controllers\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding} | admin.weddings.show | App\Http\Controllers\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding} | admin.weddings.update | App\Http\Controllers\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding} | admin.weddings.destroy | App\Http\Controllers\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/edit | admin.weddings.edit | App\Http\Controllers\WeddingsController#edit | web |
| | GET|HEAD | admin/weddings/{wedding}/menus | admin.menus.index | App\Http\Controllers\WeddingsController#index | web |
| | POST | admin/weddings/{wedding}/menus | admin.menus.store | App\Http\Controllers\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/create | admin.menus.create | App\Http\Controllers\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu} | admin.menus.show | App\Http\Controllers\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding}/menus/{menu} | admin.menus.update | App\Http\Controllers\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding}/menus/{menu} | admin.menus.destroy | App\Http\Controllers\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu}/edit | admin.menus.edit | App\Http\Controllers\WeddingsController#edit | web |
| | GET|HEAD | weddings | weddings.index | App\Http\Controllers\FrontendController#index | web |
| | GET|HEAD | weddings/{wedding} | weddings.show | App\Http\Controllers\FrontendController#show | web |
+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------+------------+
Namespacing Groups
Finally, you could go further with optimizations with introduction of namespaces (useful if you start to have more and more controllers).
You can create a folder in you app/Http/Controllers, for example Administration, where you store all of your administrative controllers.
In the web.php file, you just have to tell Laravel that the admin prefixed route group should look for controllers in the newly created folder:
Route::prefix('admin')->namespace('Administration')->name('admin.')->group(function () {
Route::resource('weddings', 'WeddingsController');
Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});
// This will map only two methods for this resource.
Route::resource('weddings', 'FrontendController')->only(['index', 'show']);
This would lead to register these routes:
+--------+-----------+--------------------------------------------+------------------------+----------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+--------------------------------------------+------------------------+----------------------------------------------------------------+------------+
| | GET|HEAD | admin/weddings | admin.weddings.index | App\Http\Controllers\Administration\WeddingsController#index | web |
| | POST | admin/weddings | admin.weddings.store | App\Http\Controllers\Administration\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/create | admin.weddings.create | App\Http\Controllers\Administration\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding} | admin.weddings.show | App\Http\Controllers\Administration\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding} | admin.weddings.update | App\Http\Controllers\Administration\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding} | admin.weddings.destroy | App\Http\Controllers\Administration\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/edit | admin.weddings.edit | App\Http\Controllers\Administration\WeddingsController#edit | web |
| | GET|HEAD | admin/weddings/{wedding}/menus | admin.menus.index | App\Http\Controllers\Administration\WeddingsController#index | web |
| | POST | admin/weddings/{wedding}/menus | admin.menus.store | App\Http\Controllers\Administration\WeddingsController#store | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/create | admin.menus.create | App\Http\Controllers\Administration\WeddingsController#create | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu} | admin.menus.show | App\Http\Controllers\Administration\WeddingsController#show | web |
| | PUT|PATCH | admin/weddings/{wedding}/menus/{menu} | admin.menus.update | App\Http\Controllers\Administration\WeddingsController#update | web |
| | DELETE | admin/weddings/{wedding}/menus/{menu} | admin.menus.destroy | App\Http\Controllers\Administration\WeddingsController#destroy | web |
| | GET|HEAD | admin/weddings/{wedding}/menus/{menu}/edit | admin.menus.edit | App\Http\Controllers\Administration\WeddingsController#edit | web |
| | GET|HEAD | weddings | weddings.index | App\Http\Controllers\FrontendController#index | web |
| | GET|HEAD | weddings/{wedding} | weddings.show | App\Http\Controllers\FrontendController#show | web |
+--------+-----------+--------------------------------------------+------------------------+----------------------------------------------------------------+------------+
You can create a new FrontEndController, and route any front end pages to that controller.
Route::get('/admin/weddings', 'WeddingController#index');
Route::get('/weddings', 'FrontEndControllerController#showWeddings');
Then you can use middlewear to diferentiate and refactor any repeated code into the Wedding model and just call that.
This is what i do when i need to separate admin and front end routes
Route::group([
'prefix' => 'admin',
'namespace' => 'Admin', // assumed
// 'as' => 'admin.'
// 'middleware' => 'admin.'
], function () {
Route::get('weddings', 'WeddingController#index');
Route::group([
'prefix' => 'wedding',
], function () {
// Wedding Management
Route::get('{wedding}/show', 'WeddingController#show'); // Updated with /show in url otherwise it will overlap create
Route::get('{wedding}/edit', 'WeddingController#edit');
Route::put('{wedding}/edit', 'WeddingController#update');
Route::delete('{wedding}/destroy', 'WeddingController#destroy');
Route::get('create', 'WeddingController#create');
Route::post('create', 'WeddingController#store');
// Here you differentiate WeddingController and MenuController
// Route::group([
// 'middleware' => 'only_applied_to_this_group',
// ], function () {
// Menu Management
Route::get('{wedding}/menus', 'MenuController#index');
Route::get('{wedding}/menu/create', 'MenuController#create');
Route::get('{wedding}/menu/{menu}', 'MenuController#show');
Route::get('{wedding}/menu/{menu}/edit', 'MenuController#edit');
Route::post('{wedding}/menu/create', 'MenuController#store');
Route::put('{wedding}/menu/{menu}/edit', 'MenuController#update');
Route::delete('{wedding}/menu/{menu}/delete', 'MenuController#destroy')
// });
});
});
Route::group([
'namespace' => 'Front',
'as' => 'front.'
], function () {
// Front-end Management
});
I tried to make it more simplified for better routes understanding, i hope this works.

Why after migrate a Laravel application on my remote server this route doesn't work?

this is the first time that I try to migrate a Laravel application from my local environment to my remote server.
So basically I have uploaded the entire content of my local Laravel 5.4 application into this folder of my remote server:
/var/www/html/HotelRegistration
Then I am trying to opening this URL into the browser (my remote server):
http://89.36.211.48/HotelRegistration/public/
and how you can see the standard Laravel homepage appear (it is expected, I yet have to replace this page with a custom one).
The problem is that in my web.xml file I have declared these routes:
Route::get('/', function () {
return view('welcome');
});
Route::get('contact', function() {
return View::make('contact');
});
Route::post('contact', 'EnquiryController#index');
Route::resource('/registration', 'RegistrationController');
The first one is the one related to the standard Laravel page that works fine, the last one is:
Route::resource('/registration', 'RegistrationController');
and it should handle HTTP GET request toward the /registration by the RegistrationController class, this controller class contains this index() method:
/**
* Metodo che mostra la pagina di registrazione di un nuovo utente albergatore.
* #return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(){
Log::info('index() START');
return view('/registration/index');
}
that opening this URL:
http://89.36.211.48/HotelRegistration/public/registration
should show a registration page. But, as you can see opening it I obtain a Not Found error message.
Into the /var/www/html/HotelRegistration/storage/logs/laravel.log file there is nothing, I expected the output of the log putted at the beginning of the previous method controller, so it seems that it doesn't not enter in this method.
The strange thing is that opening the same URL on my localhost system (http://localhost/HotelRegistration/public/registration) it works fine.
I am thinking that maybe it could be a routes problem (maybe some route is missing), if I perform this statment on my local environment:
php artisan route:list
I obtain:
+--------+-----------+----------------------------------+----------------------+------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+----------------------------------+----------------------+------------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | activate | | App\Http\Controllers\RegistrationController#activate | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | contact | | Closure | web |
| | POST | contact | | App\Http\Controllers\EnquiryController#index | web |
| | GET|HEAD | errors | errors | Closure | web |
| | POST | registration | registration.store | App\Http\Controllers\RegistrationController#store | web |
| | GET|HEAD | registration | registration.index | App\Http\Controllers\RegistrationController#index | web |
| | GET|HEAD | registration/create | registration.create | App\Http\Controllers\RegistrationController#create | web |
| | DELETE | registration/{registration} | registration.destroy | App\Http\Controllers\RegistrationController#destroy | web |
| | PUT|PATCH | registration/{registration} | registration.update | App\Http\Controllers\RegistrationController#update | web |
| | GET|HEAD | registration/{registration} | registration.show | App\Http\Controllers\RegistrationController#show | web |
| | GET|HEAD | registration/{registration}/edit | registration.edit | App\Http\Controllers\RegistrationController#edit | web |
+--------+-----------+----------------------------------+----------------------+------------------------------------------------------+--------------+
that contains this route:
| | GET|HEAD | registration | registration.index | App\Http\Controllers\RegistrationController#index | web |
that should be the route related to the previous index() method of my controller.
But I can found it also performing the same statment on my remote environment:
root#Betrivius-VPS:/var/www/html/HotelRegistration# php artisan route:list
+--------+-----------+----------------------------------+----------------------+------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+----------------------------------+----------------------+------------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | activate | | App\Http\Controllers\RegistrationController#activate | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | contact | | Closure | web |
| | POST | contact | | App\Http\Controllers\EnquiryController#index | web |
| | GET|HEAD | errors | errors | Closure | web |
| | GET|HEAD | registration | registration.index | App\Http\Controllers\RegistrationController#index | web |
| | POST | registration | registration.store | App\Http\Controllers\RegistrationController#store | web |
| | GET|HEAD | registration/create | registration.create | App\Http\Controllers\RegistrationController#create | web |
| | GET|HEAD | registration/{registration} | registration.show | App\Http\Controllers\RegistrationController#show | web |
| | PUT|PATCH | registration/{registration} | registration.update | App\Http\Controllers\RegistrationController#update | web |
| | DELETE | registration/{registration} | registration.destroy | App\Http\Controllers\RegistrationController#destroy | web |
| | GET|HEAD | registration/{registration}/edit | registration.edit | App\Http\Controllers\RegistrationController#edit | web |
+--------+-----------+----------------------------------+----------------------+------------------------------------------------------+--------------+
So what could be the problem? Why on my remote environment this URL (http://89.36.211.48/HotelRegistration/public/registration) seems not to be hanbdled as done in the local one?
What could be the problem? What am I missing? How can I try to fix it?
You need to first change the permissions and give the requires permission and run this command
composer install
or
composer update
Thank you

Laravel5 make:auth doesn't work

I used the command make:auth and it showed:
Created View: /var/www/myblog/resources/views/auth/login.blade.php
Created View: /var/www/myblog/resources/views/auth/register.blade.php
Created View: /var/www/myblog/resources/views/auth/passwords/email.blade.php
Created View: /var/www/myblog/resources/views/auth/passwords/reset.blade.php
Created View: /var/www/myblog/resources/views/auth/emails/password.blade.php
Created View: /var/www/myblog/resources/views/layouts/app.blade.php
Created View: /var/www/myblog/resources/views/home.blade.php
Created View: /var/www/myblog/resources/views/welcome.blade.php
Installed HomeController.
Updated Routes File.
Authentication scaffolding generated successfully!
It looks like great and I got a homepage showing up correctly.
see picture here
But when I was trying to access Login and Register page, something went wrong.
It gave me errors like:
Not Found
The requested URL /login was not found on this server.
Apache/2.4.7 (Ubuntu) Server at ...
and I typed php artisan route:list in terminal I got this:
+--------+-----------+-------------------------+-----------------+-----------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-------------------------+-----------------+-----------------------------------------------------------------+------------+
| | GET|HEAD | / | | Closure | |
| | GET|HEAD | article | article.index | App\Http\Controllers\ArticleController#index | |
| | POST | article | article.store | App\Http\Controllers\ArticleController#store | |
| | GET|HEAD | article/create | article.create | App\Http\Controllers\ArticleController#create | |
| | DELETE | article/{article} | article.destroy | App\Http\Controllers\ArticleController#destroy | |
| | PUT|PATCH | article/{article} | article.update | App\Http\Controllers\ArticleController#update | |
| | GET|HEAD | article/{article} | article.show | App\Http\Controllers\ArticleController#show | |
| | GET|HEAD | article/{article}/edit | article.edit | App\Http\Controllers\ArticleController#edit | |
| | GET|HEAD | home | | App\Http\Controllers\HomeController#index | web,auth |
| | GET|HEAD | login | | App\Http\Controllers\Auth\AuthController#showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\AuthController#login | web,guest |
| | GET|HEAD | logout | | App\Http\Controllers\Auth\AuthController#logout | web |
| | POST | password/email | | App\Http\Controllers\Auth\PasswordController#sendResetLinkEmail | web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\PasswordController#reset | web,guest |
| | GET|HEAD | password/reset/{token?} | | App\Http\Controllers\Auth\PasswordController#showResetForm | web,guest |
| | GET|HEAD | register | | App\Http\Controllers\Auth\AuthController#showRegistrationForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\AuthController#register | web,guest |
+--------+-----------+-------------------------+-----------------+-----------------------------------------------------------------+------------+
It's a bit different from what I expect. Cuz from what I learned in laracast.com, the route list should be like "Auth\login" instead of merely "login"... Idk what's going wrong here, is it because I'm using apache? or other configurations I didn't set up correctly... Looking for help
Also another question:
when I access www.mydomain.com, I'd expect the default welcome page instead of auth's home page but actually it gives me the auth's home page instead. But when I was trying to access www.mydomain.com/home which I expect to see that home page again, instead I received 404 not found errors which confused me a lot. here is my routes.php:
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::resource('article', 'ArticleController');
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
});
Hi Guys I just solved this problem.
This problem is definitely unrelated to laravel and is totally caused by Apache2 settings.
What I did is simply
sudo a2enmod rewrite
and restart service.

Laravel 5.2 Flash Data Does Not Persist

Should a redirect with flash data persist the flash data if the auth middleware is involved?
A few housekeeping things to note that will answer some possible followup questions:
I am calling the web middleware.
I'm using the file sessions driver.
I can retrieve values stored in the session with the exception of flashed data.
I have tried reflashing the flashed data by adding the following line to the Authenticate middleware:
$request->session()->reflash();
As such, Authenticate.php now appears as follows:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
$request->session()->reflash();
return $next($request);
}
This issue is also affecting the auth boilerplate generated by make:auth, resulting in the $errors failing to display on error.
UPDATE (3/29 # 08:54 EST)
I uncovered what I believe to have been the root cause, as you will see below. Each route was calling 'web' middleware twice. As such, two requests were actually taking place which was removing the flash message(s) before the user had a chance to see them. Original route:list is below.
+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | groups | | App\Http\Controllers\GroupsController#index | web,web,auth |
| | GET|HEAD | groups/set-default/{id} | | App\Http\Controllers\GroupsController#setDefaultGroup | web,web,auth |
| | GET|HEAD | home | | App\Http\Controllers\HomeController#index | web,web,auth |
| | GET|HEAD | login | | App\Http\Controllers\Auth\AuthController#showLoginForm | web,web,guest |
| | POST | login | | App\Http\Controllers\Auth\AuthController#login | web,web,guest |
| | GET|HEAD | logout | | App\Http\Controllers\Auth\AuthController#logout | web,web |
| | POST | password/email | | App\Http\Controllers\Auth\PasswordController#sendResetLinkEmail | web,web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\PasswordController#reset | web,web,guest |
| | GET|HEAD | password/reset/{token?} | | App\Http\Controllers\Auth\PasswordController#showResetForm | web,web,guest |
| | GET|HEAD | register | | App\Http\Controllers\Auth\AuthController#showRegistrationForm | web,web,guest |
| | POST | register | | App\Http\Controllers\Auth\AuthController#register | web,web,guest |
| | GET|HEAD | visitees | | App\Http\Controllers\VisiteesController#index | web,web,auth |
| | GET|HEAD | visitees/check-in/{id} | | App\Http\Controllers\VisiteesController#checkIn | web,web,auth |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+---------------+
My routes now appear as follows after removing the routes from the 'web' middleware:
+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | groups | | App\Http\Controllers\GroupsController#index | web,auth |
| | GET|HEAD | groups/set-default/{id} | | App\Http\Controllers\GroupsController#setDefaultGroup | web,auth |
| | GET|HEAD | home | | App\Http\Controllers\HomeController#index | web,auth |
| | GET|HEAD | login | | App\Http\Controllers\Auth\AuthController#showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\AuthController#login | web,guest |
| | GET|HEAD | logout | | App\Http\Controllers\Auth\AuthController#logout | web |
| | POST | password/email | | App\Http\Controllers\Auth\PasswordController#sendResetLinkEmail | web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\PasswordController#reset | web,guest |
| | GET|HEAD | password/reset/{token?} | | App\Http\Controllers\Auth\PasswordController#showResetForm | web,guest |
| | GET|HEAD | register | | App\Http\Controllers\Auth\AuthController#showRegistrationForm | web,guest |
| | POST | register | | App\Http\Controllers\Auth\AuthController#register | web,guest |
| | GET|HEAD | visitees | | App\Http\Controllers\VisiteesController#index | web,auth |
| | GET|HEAD | visitees/check-in/{id} | | App\Http\Controllers\VisiteesController#checkIn | web,auth |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+------------+
Upon moving the routes out of the 'web' middleware group, the flash message display correctly. But, now I have a new issue!
The flash messages are not being removed from the session after the initial request. They persist each subsequent request until they are manually flushed or forgotten.
I'm not sure at this point if I should open up a second question that specifically addresses the persisting of the flash data. Please advise if so.
please run a composer update to update laravel/framework to v5.2.27, then issue php artisan make:auth to regenerate auth routes

Categories