Laravel 5.1 logout custom message and redirect to previous page

When you’re logging out in Laravel 5 and 5.1 the AuthenticatesUsers is called and the getLogout method. Since, it’s in the Illuminate directory it’s not nice to write in this file directly. It’s better to rewrite the getLogout method from an Auth\AuthController.

Normally AuthCntroller, it might use this traits:

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

Now we’ll rename the getLogout of the AuthenticatesUsers trait:

use AuthenticatesAndRegistersUsers
    {
        getLogout as authLogout;
    }
    use ThrottlesLogins;

Now we can rewrite the getLogout and still use/inherit the old trait’s class:

    /**
     * Overwrite getLogout method of trait AuthenticatesUsers and calls it again, since it's renamed as $this->authLogout();
     * @return Response
     */
    public function getLogout()
    {
        if (!empty(URL::previous()) && !str_contains(URL::previous(), "auth/"))
        {
            $this->redirectAfterLogout = URL::previous(); // Send back to previous url if possible
        }

        alert()->success('You\'re logged out', 'Logout'); // Send a flash message e.g. with the SweetAlert package: https://github.com/uxweb/sweet-alert
 
        return $this->authLogout(); // rest of the old method of the trait
    }

2 thoughts on “Laravel 5.1 logout custom message and redirect to previous page”

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish
Scroll to Top