dongyan7988 2016-01-28 15:57
浏览 66
已采纳

Laravel身份验证不会将我设置为已通过身份验证

I'm pretty fresh into laravel. I've used CakePHP a time or two, but not this.

Currently I'm trying to make a authentication system. The registration does work, so does logging in.

When I log in, my view gets displayed. But when I try to secure the controllers functions by using the auth middleware:

public function __construct() {
    $this->middleware("auth");
}

it keeps redirecting me to /, but when it isn't there, it all works?

Also, when I try to do var_dump(Auth::check()); in the login function, it shows true, but when I do it in my index (Where i keep getting wrongfully redirected to) it shows false.

This is how I log in my users:

public function postLogin(LoginRequest $request) {
    if ($this->auth->attempt($request->only("username", "password"))) {
        return redirect("/me");
    }

    return redirect("/")->withInput()->withErrors([
        "username" => "The credentials you entered did not match out system."
    ]);
}

Is it because of a bug in the code that causes this?

Thanks in advance.

Edit: I'm making use of Laravel 5.2. And also as requested: here are the routes.

Route::group(['middleware' => ['web']], function () {
    Route::get('/', 'UnloggedController@index');
    Route::get('/me', 'MeController@index');
});

Route::controller('/','Auth\AuthController');
  • 写回答

1条回答 默认 最新

  • douluxia0606 2016-01-28 16:23
    关注

    You need to move Route::controller definition into the route group that has the web middleware added, otherwise the session will not be enabled for it and the authentication system needs the session in order to work. So it should be like this:

    Route::group(['middleware' => ['web']], function () {
        Route::get('/', 'UnloggedController@index');
        Route::get('/me', 'MeController@index');
    
        Route::controller('/','Auth\AuthController');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?