问题
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