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 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'