Laravel viaRemember always returning false

余生颓废 提交于 2021-02-20 04:16:14

问题


I've had trouble with keeping my users logged in, so I did some testing and I noticed that the function viaRemember always returns false.

This is my login controller function with all the irrelevant code stripped off:

public function postSignin(Request $request, AppMailer $mailer) {
    $this->validate($request, [
        'email' => 'required',
        'password' => 'required',
    ]);     

    $username = $request->get('email');
    $password = $request->get('password');

    $field = filter_var($username,FILTER_VALIDATE_EMAIL)? 'email': 'username';      

    Auth::attempt([$field => $username, 'password' => $password], true);


    return redirect()->back()->with('info', 'You are now signed in.');
}       

Works fine. As you can see the 2nd argument in Auth::attempt is set to true by default.

In my database, the value for "remember_token" is changed at every login, so that appears to be working. However, if I do dd(Auth::viaRemember()); the result will always be false.

I tried changing my config/session.php settings as well, switching some values around, but that doesn't seem to do much.

Why is this happening?


回答1:


I think you misunderstood this feature. "viaRemember" is to check whether the returning user is authenticated via "remember me" function. Definitely the first time you login with "remember me" checked this will return false. Try closing your browser and open up again it will return true.

Or you can do its behavior by going in config\session.php and change

'expire_on_close' = false

to true and everything became fine like you thinking



来源:https://stackoverflow.com/questions/49461805/laravel-viaremember-always-returning-false

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