65

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/

Tim van Uum
  • 1,873
  • 1
  • 17
  • 36

20 Answers20

89

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.

Avram
  • 1,676
  • 13
  • 15
  • Hi @avram, I have a question regarding the syntax in `use … { … as … }`. It looks like a bit of import/deconstucting. Do you know what is the name of this syntax/pattern? Thanks for your help, it works on the project I’m working on! – meduz' May 08 '19 at 14:12
  • 2
    @meduz' I'm not quite sure how it's called, I've just found it in the PHP docs: https://www.php.net/manual/en/language.oop5.traits.php#language.oop5.traits.conflict – Avram May 08 '19 at 21:22
  • 3
    The only thing that is not mentioned is including ``use Illuminate\Http\Request;`` in the LoginController. – Naser.Sadeghi Sep 08 '20 at 10:45
  • Works for me in Laravel 8 as well. Thanks. – Strabek Mar 08 '22 at 23:58
64

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');
}
Chris
  • 54,599
  • 30
  • 149
  • 186
15

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.

kalatabe
  • 2,909
  • 2
  • 15
  • 24
  • 3
    Was already using this. But no need to extend LoginController since controllers are open for modification. Just add the method to LoginController. Cause the method is in a trait that's used in the LoginController. This seems to be the solution, a redirect is not being triggered inside an event so events isn't an option – Tim van Uum Sep 05 '16 at 11:25
  • 1
    You're right, it's perfectly fine to define the `logout` override in the LoginController itself, I just like to keep stuff that didn't come "built-in" in my own separate files. – kalatabe Sep 05 '16 at 11:40
  • 1
    works like a charm in laravel 5.3, but you can directly edit the LoginController and override the logout method - anyway it would be nice if it would be implemented like the redirect after successfull login – Ilario Engler Oct 12 '16 at 11:43
  • @Kaloyan Doichinov, It seems you master laravel. I need you help. Look here : http://stackoverflow.com/questions/41047583/how-to-add-dynamic-dropdown-list-column-on-laravel-5-3-registration – moses toh Dec 09 '16 at 03:07
10

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.

joan16v
  • 5,055
  • 4
  • 49
  • 49
Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
8

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

SMPLYJR
  • 854
  • 1
  • 11
  • 23
6

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

Mohamed Gabr
  • 716
  • 6
  • 18
4

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');
}
ArtemSky
  • 1,173
  • 11
  • 20
3

Just use this in routes/web.php

Route::get('logout', function (){
Auth::logout();
return redirect('your URL');
});
Xander
  • 33
  • 3
2

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

Andrej
  • 7,474
  • 1
  • 19
  • 21
  • Found the event and it triggered. But something keeps it from redirecting. Inside the event I had `return redirect()->route('admin::login');` but it is not triggered. Event would be at least a bit cleaner. Also using a normal `header('Location: /');` did not work. – Tim van Uum Sep 05 '16 at 11:27
2

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 : '/');
}
cyclops1101
  • 422
  • 3
  • 8
1

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
Aranya Sen
  • 974
  • 8
  • 13
1

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');
}
Omid Ahmadyani
  • 1,430
  • 13
  • 15
1

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');;
}
1

Illuminate Support package: override logout method in LoginController.php

 use Illuminate\Support\Facades\Auth;

 public function logout()
     {
         Auth::logout();

     return redirect('/wherever/you/want');
 }
Florian
  • 2,796
  • 1
  • 15
  • 25
0

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');
}
Murad Shukurlu
  • 417
  • 6
  • 11
0

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

0

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)

bvanlew
  • 1,465
  • 16
  • 13
0

It'll work 100% on laravel 9. When you manaully redirect to any route after logout. import this classes at top of the LoginController.

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

 protected function logout(Request $request)
    {
        $this->guard()->logout();
        Auth::logout();
        Session::flush();
        Session::regenerate();
        return redirect('/login');
    }
Kaleemullah
  • 446
  • 3
  • 8
0

In Laravel 8+, try editing the redirect method in logout method on /vendor/laravel/ui/auth-backend/AuthenticateUsers.php Laravel-AuthenticateUsers.php

-5

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

z.gomar
  • 372
  • 2
  • 11
  • 8
    This is very bad practice! If you run `composer update` your change is gone. Never alter files in the vendor folder. – Tim van Uum Nov 24 '16 at 09:58