Laravel Session::getId() is different on login and logout

柔情痞子 提交于 2020-01-16 09:08:10

问题


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

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