问题
i have table user_logins to maintain multiple login information about user
table structure for user_logins :
'session_id',
'user_id',
'ip_address',
'user_agent',
'browser_name',
'location',
'login_at',
'is_active'
i have UserEventSubscriber listener with 2 functions like below :
public function handleUserLogin($event) {
UserLogin::create([
'session_id'=>Session::getId(),
...
]);
}
im getting Session::getId() = mpT6RDsl54JExkejrqf3fnYiFLzbR2pTb2qfNHBe in handleUserLogin function
now when user logout i want to update / delete the table entry from user_logins table where session_id = Session::getId()
public function handleUserLogout($event) {
dd(Session::getId());
//UserLogin::where('session_id',Session::getId())->delete();
}
on handleUserLogout function im getting a different session id
Session::getId() qLYngAx1Vs8VBhxm0oCKZO3fDwun02UEXRyDm0Hi so im unable to update/delete entry in user_logins table
i have seen sessions table have id qLYngAx1Vs8VBhxm0oCKZO3fDwun02UEXRyDm0Hi which is same as id i get on logout function .
so my question is why im getting a different session id on user login function ? and what should i do to get same session id using Session::getId() in handleUserLogin and handleUserLogout function .
回答1:
I have found that , need to override sendLoginResponse from AuthenticatesUsers in Auth/LoginController
$request->session()->regenerate() generates new session id after login , so if you want to get same id as before , comment out that line .
protected function sendLoginResponse(Request $request)
{
// $request->session()->regenerate(); <-- this line is generating new session after user login
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
来源:https://stackoverflow.com/questions/56856525/laravel-sessiongetid-is-different-on-login-and-logout