The secure Logging out process is too much necessary for any system. We should ensure that we terminate all the session and other important browser stuff while logging out. Most of the time, during log out, we have to use Auth::logout() process, which is not a safe idea. Let's get to drive the secure logging-out procedure.
Step 1: Make a logout Route
Just make a route on web.php. In our case, we make it like the
below. Here our route can be get or post. Nothing specific that you can do on
your own.
Route::get('/logout',[TestController::class,'logout'])->middleware('auth');
Step 2: Logout method on the controller
Now make a method you will hit by the “/logout” route. All the
things will be like below. In your case, you can change the returned JSON data.
public
function cLogout(Request
$request){
Auth::logout();
$result = $request->session()->invalidate();
if ($result){
$request->session()->regenerateToken();
return response()->json([
'status'=>1,
"message"=>"You are Logged out"
]);
}
}
Now you can hit the “/logout” route to log out from the
system. But make sure the route is wrapped adequately by the ->Middleware(“auth”).
And that’s all about logging out. Most of the time, people miss
terminating their old cookies and other session based data. Trust me that can
make a super threat to your application. Don’t leave without terminating or regenerating.
Leave your comment below about this process.