Laravel 5.2 Login not working

只谈情不闲聊 提交于 2019-12-05 22:34:55

That happens because your '/' route is not under the web middleware group and you're not using the auth middleware for the routes you want to be authenticated

This means that when accessing that route, session is not available, and the auth middleware doesn't fire, so authentication doesn't work.

Try to put all the routes in which you want to use session under the web middleware group, and for the routes in which you want authentication use the auth middleware:

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

    /* these routes use 'auth' middleware, so only an authenticated user will access*/
    Route::group( ['middleware' => 'auth' ], function () {
        Route::get('/', 'BaseController@index');
    });

    Route::auth();
});

A Note on Laravel version:

Keep in mind that if you're using Laravel version >= 5.2.27 the middleware web is used by default on every route defined in routes.php as you can see 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');
    });
}

In that case you can remove you statement for using the web middleware group, but you'll only need to use auth middleware for the authenticated routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!