Laravel 5.2 Auth Not Working

Laravel 5.2 Auth not Working

Laravel 5.2 introduces the middleware groups concept: you can specify that one or more middleware belongs to a group, and you can apply a middleware group to one or more routes

By default Laravel 5.2 defines a group named web, used to group the middleware handling session and other http utilities:

protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],

So, if you want session handling, you should use this middleware group for all the routes in which you want to use authentication:

Route::group( [ 'middleware' => ['web'] ], function () 
{
//this route will use the middleware of the 'web' group, so session and auth will work here
Route::get('/', function () {
dd( Auth::user() );
});
});

UPDATE FOR LARAVEL VERSION >= 5.2.27

As of Laravel 5.2.27 version, all the routes defined in routes.php are using by default the web middleware group. That is achieved in app/Providers/RouteServiceProvider.php :

protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web'
], function ($router) {
require app_path('Http/routes.php');
});
}

So you don't need anymore to add manually the web middleware group to your routes.

Anyhow, if you want to use the default authentication for a route, you still need bind the auth middleware to the route

Laravel 5.2 Auth login works, Auth::check fails

So I ended up figuring out the solution, I just wish this wasn't down-voted. But oh well, on my model I never indicated a primary key and as such when retrieving the user it was not possible.

To fix for me it simply required this line

 protected $primaryKey = 'client_idx_NW';

Laravel 5.2 /login & /register not working

You were logged in & cookies were keeping track of your authentication status.
Thus you are redirected to home page when you visit

/login
/register

In Laravel Auth, Logged-In users can't view login page or register page. You have to either logout OR delete the cookies (not a recommended way) to view these pages again.



Related Topics



Leave a reply



Submit