29

I have a "Logout" link in my top navigation bar. I'm wondering how I can make it so that while I'm logged in, it'll log me out when I click on it and return me to the homepage.

To be specific, what changes to which files do I make in Laravel? Also, what code do I need to write in the view, which currently contains just HTML, to trigger this?

7 Answers 7

87

When you run php artisan make:auth, the default app.php in Laravel 5.5 does it like this:

<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
    Logout
</a>

<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
    {{ csrf_field() }}
</form>
3
  • 2
    This is the correct way you should be logging out in Laravel. The accepted answer does not protect against cross site request forgery (albeit, logging someone out may not be your biggest concern, you should be doing it the right way).
    – Colin
    Jan 29, 2019 at 14:47
  • 2
    You can use @csrf to replace {{ csrf_field() }} for Laravel 5.6 and above
    – Fred Lai
    Jan 29, 2020 at 1:17
  • Because I am accustomed to including CSRF token in all forms in my app, I should not get difficulty in applying this way. The difference is I use React with React Router in the front end. But the concept is still the same
    – Lex Soft
    Mar 4, 2020 at 12:54
37

Edited 28/12/2019: It's work, but This answer contains a serious security issue. Please consider before using it. The Answer by Lucas Bustamante maybe a better choice. Refer to the comment section of this answer.

1) if you are using the auth scaffold that laravel contains. You can do this, in your navigation bar add this:

<a href="{{ url('/logout') }}"> logout </a>

then add this to your web.php file

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

Done. This will logout you out and redirect to homepage. To get the auth scaffold, from command line, cd into your project root directory and run

php artisan make:auth 

2) add this to your navigation bar:

<a href="{{ url('/logout') }}"> logout </a>

then add this in your web.php file

Route::get('/logout', 'YourController@logout');

then in the YourController.php file, add this

public function logout () {
    //logout user
    auth()->logout();
    // redirect to homepage
    return redirect('/');
}

Done.

Read:

https://mattstauffer.co/blog/the-auth-scaffold-in-laravel-5-2
https://www.cloudways.com/blog/laravel-login-authentication/
8
  • @BenKao Glad i could help. Please mark the answer as correct
    – Dammy joel
    Jun 7, 2017 at 19:34
  • 10
    Woah, this is a big no no. You should have a form that submits a post request with a CSRF token field. Allowing logouts via a GET request allows any website to log you out!
    – Colin
    Jan 29, 2019 at 1:30
  • 1
    @ColinLaws I agree it's a no-no, and there's another reason in addition to the one you state: some browsers pre-cache links on the page when you visit them, which could lead to you getting logged out just by visiting a page with that link on it. See stackoverflow.com/a/14587231/2778502
    – jeff-h
    Jul 15, 2019 at 0:47
  • @jeff-h Little to my surprise, Identity server 4 has some way of doing this. It is unrelated, as this is Laravel, but I thought it was interesting to find out a real way to logout using a GET.
    – Colin
    Jul 16, 2019 at 2:22
  • 1
    this dont work for me because this error The GET method is not supported for this route. Supported methods: POST. Oct 5, 2019 at 16:20
8

Use the logout() method:

auth()->logout();

Or:

Auth::logout();

To log users out of your application, you may use the logout method on the Auth facade. This will clear the authentication information in the user's session.

2
  • Then OP can just add: return redirect('/'); Mar 29, 2017 at 8:10
  • What changes to which files do I make in Laravel? Like where do I write the code Auth::logout() and return redirect('/')? Also note that my "Logout" link has to trigger all this. How does the logout link trigger these actions?
    – Ben Kao
    Mar 29, 2017 at 8:22
6

if you want to use jQuery instead of JavaScript:

<a href="javascript:void" onclick="$('#logout-form').submit();">
    Logout
</a>

<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
    @csrf
</form>
4

As the accepted answer mentions that logging out via GET has side effects you should use the default POST route already created by Laravel auth.

Simply create a little form and submit it via link or button HTML tag:

<form action="{{ route('logout') }}" method="POST">
    @csrf
    <button type="submit">
        {{ __('Logout') }}
    </button>
</form>
0

If you use guard you can logout using this line of code :

Auth::guard('you-guard')->logout();
0

in laravel 8.x

@csrf
                        <x-jet-dropdown-link href="{{ route('logout') }}"
                                            onclick="event.preventDefault();
                                                        this.closest('form').submit();">
                            {{ __('Logout') }}
                        </x-jet-dropdown-link>
                    </form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.