I
I
Ivan2022-03-20 22:24:36
Laravel
Ivan, 2022-03-20 22:24:36

Laravel how to log out a user during authorization?

I want to make it so that when the user logs in, he is thrown out of all other devices.

public function login(Request $request) {    
        // auth()->logout();
        Auth::logout();
        $phone = $request->phone;    
        $user = User::where('phone', $phone)->first();    
        if ($user) {    
            Auth::login($user, true);    
            return redirect()->route('catalog.index');    
        }
        else {    
            return redirect()->back();    
        }    
    }

Did it this way (LoginController file) but it doesn't work. I logged in from one browser, then tried to log in again from another and was still authorized in the first browser. How can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dsmoke, 2022-03-20
@youmixx

you need to authorize the user later:
Auth::logoutOtherDevices('password');
https://laravel.com/api/9.x/Illuminate/Support/Fac...

public function login(Request $request) 
{    
     $phone = $request->phone;  
     $user = User::where('phone', $phone)->first();   

     if ($user) {    
            Auth::login($user, true); 
            Auth::logoutOtherDevices($request->password);

            return redirect()->route('catalog.index');    
     }
     else {    
            return redirect()->back();    
     }    
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question