Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set laravel 5.3 logout redirect path?

Is there no elegant solution to redirect to a specific page after logging out in Laravel 5.3?

The function being called is from the trait AuthenticatesUsers:

public function logout(Request $request) {     $this->guard()->logout();      $request->session()->flush();      $request->session()->regenerate();      return redirect('/'); } 

This is a default function from the core of laravel. So I have to override the whole function I cannot edit the core. But isn't there a more simpler solution, cause it feel like overkill to manually logout, flush and regenerate again.

Worked the answers out in an article: https://codeneverlied.com/how-to-set-logout-redirect-path-in-laravel-5-8-and-before/

like image 375
Tim van Uum Avatar asked Sep 05 '16 09:09

Tim van Uum


People also ask

How do I redirect after logout?

To redirect the user after they log out from a specific application, you must add the URL used in the returnTo parameter of the redirect URL to the Allowed Logout URLs list in the Settings tab of your Auth0 application that is associated with the CLIENT_ID parameter.


1 Answers

This is how I did it. In Auth\LoginController you have:

use AuthenticatesUsers; 

Change it to:

use AuthenticatesUsers {     logout as performLogout; } 

Then, define a new logout() method in your LoginController:

public function logout(Request $request) {     $this->performLogout($request);     return redirect()->route('your_route'); } 

Sure, regular logout() method in that trait has only 3 lines (used to log users out of the system) so you can copy them to your method, but you should always follow the DRY principle (don't repeat yourself) and re-use as much code as you can.

like image 187
Avram Avatar answered Sep 16 '22 14:09

Avram