How to log out and redirect it to the login page using Laravel 5.4?

I am using Laravel 5.4 and trying to implement an authentication system. I used the php artisan make: auth command to configure it. I edited the views according to my layout. Now when I try to log out, I selected this error

NotFoundHttpException in line RouteCollection.php 161:

can anyone help me log out?

+33
source share
8 answers

In your web.php (routes):

add:

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

In LoginController.php

add:

 public function logout(Request $request) { Auth::logout(); return redirect('/login'); } 

Also at the top of LoginController.php , after namespace

add:

 use Auth; use Illuminate\Http\Request; 

You can now exit using yourdomain.com/logout URL or if you created a logout button add href to /logout

+106
source

Well, even if what @Tauras offers just works, I don’t think this is the right way to handle this.

You said you ran php artisan make:auth , which also had to insert Auth::routes(); into your routes/web.php routing files. Which comes with the default logout path already set and is called logout .

You can see it here on GitHub , but I will also post the code here for simplicity:

  /** * Register the typical authentication routes for an application. * * @return void */ public function auth() { // Authentication Routes... $this->get('login', 'Auth\ LoginController@showLoginForm ')->name('login'); $this->post('login', 'Auth\ LoginController@login '); $this->post('logout', 'Auth\ LoginController@logout ')->name('logout'); // Registration Routes... $this->get('register', 'Auth\ RegisterController@showRegistrationForm ')->name('register'); $this->post('register', 'Auth\ RegisterController@register '); // Password Reset Routes... $this->get('password/reset', 'Auth\ ForgotPasswordController@showLinkRequestForm ')->name('password.request'); $this->post('password/email', 'Auth\ ForgotPasswordController@sendResetLinkEmail ')->name('password.email'); $this->get('password/reset/{token}', 'Auth\ ResetPasswordController@showResetForm ')->name('password.reset'); $this->post('password/reset', 'Auth\ ResetPasswordController@reset '); } 

Note again that logout requires POST as an HTTP request method. There are many good reasons for this, but one very important point is worth mentioning: in this way you can prevent cross-site request forgery .

Thus, according to what I just pointed out, the correct way to implement this could be as follows:

 <a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('frm-logout').submit();"> Logout </a> <form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;"> {{ csrf_field() }} </form> 

Finally, note that I inserted the "ready" function out of the box " {{ csrf_field() }} !

+41
source

In the controller you can use the following:

 return redirect('login')->with(Auth::logout()); 
+8
source

here is another way to do this by calling Auth :: logout () in the route

 Route::get('/logout', function(){ Auth::logout(); return Redirect::to('login'); }); 
+3
source

In 5.5

adding

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

works fine for my routes file.

+1
source

I recommend sticking with Laravel authentication routes in web.php: Auth::routes()

This will create the following route:

 POST | logout | App\Http\Controllers\Auth\ LoginController@logout 

You need to log out using the POST form. Thus, you will also need the CSRF token, which is recommended.

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

The best way for Laravel 5.8

100% worked

Add this function to your Auth \ LoginController.php

 use Illuminate\Http\Request; 

Also add this

 public function logout(Request $request) { $this->guard()->logout(); $request->session()->invalidate(); return $this->loggedOut($request) ?: redirect('/login'); } 
0
source

If you used auth scaffolding in 5.5, just point your href to:

 {{ route('logout') }} 

No need to change routes or controllers.

-2
source

Source: https://habr.com/ru/post/1267090/


All Articles