Ved21212's avatar

Issue in logout for two different user of two different guard

Could anyone help me with handling this situation

public function logout(Request $request)
    {
        $sessionKey = $this->guard()->getName();
        $this->guard('sellerbranch')->logout();

        $request->session()->invalidate();

        return redirect('sellerbranch/login');
    }


    protected function guard()
    {
        return Auth::guard('sellerbranch');
    }

0 likes
4 replies
ahmeddabak's avatar

say that you have two guard sellerbranch and users, you can write your logout function like this in the LoginController

 public function logout(Request $request)
    {
        Auth::guard('sellerbranch')->logout();
        Auth::guard('users')->logout();

        $request->session()->invalidate();

        return $this->loggedOut($request) ?: redirect('/sellerbranch/login');
    }

if you want you can check which guard is logged in and logout only that guard, but i used this snipped in one of my projects, i never had any problems loggin out both of them.

Ved21212's avatar

Actually my issue is all the sessions get destroyed once I logout from any one of the guard can that be prevented? @ahmeddabak

ahmeddabak's avatar

Remove this line which destroy the session and make a new one $request->session()->invalidate();

Please or to participate in this conversation.