13

I'm working with Laravel 5 authentification system provided by default. After logging out, a user is redirected to the root page but I'd like to change that. I managed to do it for the "login" and "registering" process by defining "$redirectTo" in "AuthController.php". But for "logout", I defined "$redirectAfterLogout" at the same place but it seems to not be taken into account.

Could anyone explain me where is the problem and how to fix it please? Thanks a lot.

kururin
  • 199
  • 1
  • 2
  • 10

8 Answers8

32

For Laravel 5,

Open AuthController class : app/Http/Controllers/Auth/AuthController.php

Add below property to the class

protected $redirectAfterLogout = 'auth/login';

you can change auth/login with any url.

kartavya
  • 343
  • 2
  • 5
  • That's strange. I already tried that with no success... but when I tried again a few minutes ago, it works. Thank you! – kururin May 07 '15 at 11:41
  • @kururin Thats because it was just recently added... https://github.com/laravel/framework/commit/aa1204448a0d89e2846cbc383ce487df6efd9fc8 – lukasgeiter May 07 '15 at 14:47
  • 1
    And how can I do this in Laravel 5.3, it doesn't contain AuthController.php – Sunny Kumar Jan 08 '17 at 08:23
  • @SunnyKumar, I was wondering the same.. Have you found a solution? – Vinny Feb 20 '17 at 07:36
  • @SunnyKumar, I've found a solution, check the answer on this question: http://stackoverflow.com/questions/40114406/how-to-logout-and-redirect-to-login-page-using-laravel-5-3 – Vinny Feb 20 '17 at 09:48
  • Apparently, this does not work for Laravel 5.3+ for some reason. The only way is to override the function. – Ru Chern Chong Jun 26 '17 at 08:50
3

The redirect after logout is hard coded in the trait AuthenticatesAndRegistersUsers. You can override it in your AuthController by adding this:

public function getLogout()
{
    $this->auth->logout();

    return redirect('logout');
}
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • Yes, that was my first attempt. It works but I thought there was a better way to do it. Thank you! – kururin Apr 22 '15 at 12:36
3

If you don't provide the $redirectAfterLogout attribute, it will use the default which is '/'.

This logic can be found in this class: \vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php

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

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

Having said that, just add this attribute in your AuthController:

protected $redirectAfterLogout = '/afterRedirectURL';
geckob
  • 7,680
  • 5
  • 30
  • 39
1

For Laravel 5.5 override logout method inside LoginController. In my case I am redirecting to home route after login.

/**
 * 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();

    return redirect()->route('home');
}
Farid Movsumov
  • 12,350
  • 8
  • 71
  • 97
0

using the built in laravel Auth in the controllers I just override the loggedOut method which triggers after logout to redirect

in the "LoginController.php" which uses

use AuthenticatesUsers;

in the AuthenticatesUsers Trait is a logout method, you can optionally override this or you will see that it triggers a loggedOut method

You can override the logged out method which is default blank and have that redirect

    /**
     * The user has logged out of the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return mixed
     */
    protected function loggedOut()
    {
        return redirect()->route('login.show');
    }
XenitXTD
  • 59
  • 1
  • 6
-1

In App\Controllers\Auth\AuthController, add the following two variables.

protected $redirectTo = '/private_dashboard';
protected $redirectAfterLogout = '/public_homepage';

You get the idea.

anonym
  • 4,460
  • 12
  • 45
  • 75
-1

I have a same problem in Laravel 5.0. Override a method does the trick.

1) Go to app/Http/Controllers/Auth/AuthController.php 2) Add a new method :

// Override Logout method (define custom url)
public function getLogout()
{
    $this->auth->logout();
    return redirect('auth/login');  // Your Custom URL
}
Fawwad Shafi
  • 33
  • 2
  • 10
-2

it'only laravel versi 5.4 if you want custom redirect url logout, open /your-project-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php and edit redirect based on you needed

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

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

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

        return redirect('/login');
    }
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
Freddy Sidauruk
  • 300
  • 6
  • 17
  • Vendor files should not be committed or edited. This will break on your next composer update. – Maxim Kumpan Aug 01 '17 at 07:29
  • It is actually correct function, but wrong file, in L5.4, you should overwrite this method to Http/Controllers/Auth/LoginController.php – CheeHow Aug 04 '17 at 09:57