Ad

Laravel 5.2 Auth Not Working

As you guys know Laravel 5.2 was released a few days ago. I am trying this new version. I made a new project using the following command on CLI:

laravel new testapp

As per documentation of Authentication Quickstart, I followed the following command to scaffold routes and views of authentication:

php artisan make:auth

It worked fine. Registration is working fine. But I am facing problem in Login. After login I tested following in route.php file:

   Route::get('/', function () {
    dd( Auth::user());
    return view('welcome');
});

Auth::user() is returning null and also Auth::check() and Auth::guest() are not working appropriately. I have tried same thing again and again two three times by making new projects but couldn't get the correct results.

Below is the complete route.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 () {
    dd( Auth::());
    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::group(['middleware' => ['web']], function () {
    //
});

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/home', '[email protected]');
});

Can anyone help me? or Is anyone facing the same problem? How can I fix it?

Ad

Answer

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

Ad
source: stackoverflow.com
Ad