1

Everything in this login is working fine but if a user tries logout route directly

localhost:8000/logout it shows the following error

enter image description here

i want a way so that i can redirect to login if the route is called directly, what could be the vest way for it.

3 Answers3

1

define a route like this in your routes file. It is because only post method exist for logout.

Route::get('logout', function() {
   return redirect()->route('login');
});
aimme
  • 6,385
  • 7
  • 48
  • 65
0

A word of caution: As Laurel mentioned in this comments, using GET request for Logout is a bad idea, Read this discussion on StackOverflow before proceeding.

Where is the problem?

The error message is self explanatory.

The logout route doesn't support HTTP Method GET, but POST.

What is the solution?

You can define a GET route for logout, if you really wants to do it.

Route::get('logout', function() {
   // Do whatever you want to do with Logout
  Auth::logout(); // logout the user
  return redirect()->route('login'); // redirect the user to login page
});
ash__939
  • 1,614
  • 2
  • 12
  • 21
  • 1
    This is a bad idea as having a GET method for logout will allow any random website to silently log you and your users out (by setting an image’s source to the logout GET URL for example). In fact this is why Laravel changed logout to be a POST (as older versions used GET). – Laurel Jul 31 '20 at 12:34
  • Yeah! You're right. But the author of this question, wants to do it with a GET request. Anyway, I will update my answer with this note. Thanks for the information @Laurel – ash__939 Jul 31 '20 at 14:36
0
Route::get('check_logout','ExampleController@check_logout');
Route::get('logout',function(){
    return redirect('login');
});
  • 1
    Welcome to StackOverflow! This can be a solution, but could you please add an explanation about the code provided? – xKobalt Jul 31 '20 at 11:17