YasseMoh's avatar

keep user logged in after closing the browser

Firstly, Is it safe to keep users logged in using cookies? I tried to keep users logged in so they needn't login everytime they open the browser. I did some changes in the authenticatesUsers.php trait as follows but users still log out when they close the browser. How can I do that?

    /**
     * Send the response after the user was authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
     */
    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        if ($response = $this->authenticated($request, $this->guard()->user())) {
            return $response;
        }

        $ID=auth()->user()->id;$NAME=auth()->user()->name;
        session()->put(['user_id'=>$ID,'user_name'=>$NAME]);
        $sess=session()->get('user_id');
        Cookie::queue('sessCook',$sess,3600*24*30*5);   
        
        return $request->wantsJson()
                    ? new JsonResponse([], 204)
                    : redirect()->intended($this->redirectPath());
    }

0 likes
6 replies
jlrdw's avatar

@afrasiyab_haider good answer, same as mine but you showed code.

I am really trying to get folks to lookup stuff for themselves in the documentation. Laravel has some great documentation.

Snapey's avatar

why try and set your own cookie when laravel supports what you want by default?

1 like

Please or to participate in this conversation.