How to set laravel 5.3 logout redirect path?

PhpLaravelLaravel 5.3

Php Problem Overview


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/

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

Laravel > 5.7

The accepted answer is fine, but you can completely bypass touching any of the logout logic by simply overwriting the loggedOut method:

// App\Http\Controllers\Auth\LoginController.php
protected function loggedOut(Request $request) {
    return redirect('/where/ever/you/want/to/go');
}

Solution 3 - Php

I would inherit LoginController and override the logout function coming from the trait in there:

LoginController.php -> leave that as it is.

MyLoginController.php:

class MyLoginController extends LoginController {

protected $redirectAfterLogout = '/goodbye';

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

Of course, you should remember to update your Auth routes accordingly.

Solution 4 - Php

I'm using Laravel-5.2, what I used was:

public function logout()
{
    Auth::logout();
    Session::flush();
    return redirect('/');
}

Make sure you have imported:

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

In your controller.

Solution 5 - Php

Assuming someone is viewing it now a days and the version of the laravel they are using is 5.7

Add this line in LoginController.php

public function logout()
{
    Auth::logout();
    return redirect()->to('/your-route');
}

This assumes you are using the out of the box authentication module provided by laravel

Solution 6 - Php

The Auth::routes method in laravel 5.3 registers a POST route for /logout instead of a GET route. This prevents other web applications from logging your users out of your application. To upgrade, you should either convert your logout requests to use the POST verb or just register your own GET route for the /logout URI by adding this route to the file Routes/web.php:-

Route::get('/logout', 'Auth\LoginController@logout');

and it should work fine and redirect you to the '/' directory as it's defined in the LoginController.php

Quoted from:-

https://laravel.com/docs/5.3/upgrade

Solution 7 - Php

The simplest way is to override logout trait at LoginController in App\Http\Controllers\Auth\LoginController like this

public function logout(Request $request){
    $this->guard()->logout();
    $request->session()->flush();
    $request->session()->regenerate();

    return redirect()->route('you_route_name');
}

Solution 8 - Php

Just use this in routes/web.php

Route::get('logout', function (){
Auth::logout();
return redirect('your URL');
});

Solution 9 - Php

Every logout action fires an event Events\Logout. You can create a listener that listens to this event and add some logic to there. See more about listeners here https://laravel.com/docs/5.3/events

Solution 10 - Php

If you're using the out of the box AuthController add this variable to the top and then change the string to redirect wherever you want.

protected $redirectAfterLogout = '/';

The AuthenticatesUsers class has a logout function that checks for this variable.

public function logout()
{
        Auth::guard($this->getGuard())->logout();

    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}

Solution 11 - Php

To avoid repeating logout code and to follow DRY, you may

  • Add a custom logout route in Auth/LoginController, call Auth::logout() and return redirect to your path, or
  • Add an after-middleware (say redirectAfterLogout) and add it to the logout route

Solution 12 - Php

The accepted answer is fine, but you can completely bypass touching any of the logout logic by simply overwriting the loggedOut method:

protected function loggedOut(Request $request) {
    return redirect('/where/ever/you/want/to/go');
}

Solution 13 - Php

In Laravel 5.8 find the below path :

App\Http\Controllers\Auth\LoginController.php

use Illuminate\Http\Request;*

write this function

public function logout(Request $request){
    $this->guard()->logout();

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

    return $this->loggedOut($request) ?: redirect('/Where/You/want/to/redirect');;
}

   

Solution 14 - Php

Illuminate Support package: override logout method in LoginController.php

 use Illuminate\Support\Facades\Auth;

 public function logout()
     {
         Auth::logout();
 
     return redirect('/wherever/you/want');
 }

Solution 15 - Php

If we review Laravel's default logout function's codes on AuthenticatesUsers trait (which located under vendor/laravel/ui/auth-backend/AuthenticatesUsers for laravel 7) we will see a hard coded path for redirecting. It means we need to change the url from the source but editing files via vendor is a wrong way however traits provide us with a better way which we are in power of overriding the codes So just paste the all logout function's codes to LoginController page and change the redirect('/') function's paramatr to any url you want. Currently my codes is look like those just place it on LoginController:

/**
 * Log the user out of the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function logout(Request $request)
{
    $this->guard()->logout();

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

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

    if ($response = $this->loggedOut($request)) {
        return $response;
    }

    return $request->wantsJson()
        ? new Response('', 204)
        : redirect('/login');
}

Solution 16 - Php

In my case I wanted it to redirect the user to the home page '/home' after logging out but it kept redirecting him to login page
The reason was that the default HomeController used the auth middleware

I had to remove this line $this->middleware('auth'); from the __construct method

Solution 17 - Php

I just updated an old website to Laravel 5.3. This involved adding the 4 new Auth controllers from Laravel Github as advised in the upgrade guide.

Three of new controllers: RegisterController.php, LoginController.php and ResetPaswordController.php all contain a code block

/**
 * Where to redirect users after resetting their password.
 *
 * @var string
 */
protected $redirectTo = '/home';

Simply change /home to the desired redirect location. At least that worked for me in Laravel 5.3 (data April 2021)

Solution 18 - Php

you can go to vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php in function logout change return redirect('/'); to your route address.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTim van UumView Question on Stackoverflow
Solution 1 - PhpAvramView Answer on Stackoverflow
Solution 2 - PhpChrisView Answer on Stackoverflow
Solution 3 - PhpkalatabeView Answer on Stackoverflow
Solution 4 - PhpSachith MuhandiramView Answer on Stackoverflow
Solution 5 - PhpSMPLYJRView Answer on Stackoverflow
Solution 6 - PhpMohamed GabrView Answer on Stackoverflow
Solution 7 - PhpArtemSkyView Answer on Stackoverflow
Solution 8 - PhpXanderView Answer on Stackoverflow
Solution 9 - PhpAndrejView Answer on Stackoverflow
Solution 10 - Phpcyclops1101View Answer on Stackoverflow
Solution 11 - PhpAranya SenView Answer on Stackoverflow
Solution 12 - PhpOmid AhmadyaniView Answer on Stackoverflow
Solution 13 - PhpZaheer SadiqueView Answer on Stackoverflow
Solution 14 - PhpOussama OuardiniView Answer on Stackoverflow
Solution 15 - PhpMurad ShukurluView Answer on Stackoverflow
Solution 16 - PhpSoundous BahriView Answer on Stackoverflow
Solution 17 - PhpbvanlewView Answer on Stackoverflow
Solution 18 - Phpz.gomarView Answer on Stackoverflow