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');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题