Search code examples
laravelauthenticationrouteslaravel-blademiddleware

auth logout doesn't work laravel


I made the files for authentication using the command

php artisan make:auth

I've read on the internet that register, login, as well as logout should work properly, but localhost:8080/logout doesn't work, and I don't know why.

I also read something about modifying AuthController in app, but I do not have that file.

I tried to do it by hand, which means I created a middleware LogoutRedirect:

 public function handle($request, Closure $next)
{
    return redirect(pages.logout);
}

In the routes I added

use App\Http\Middleware\LogoutRedirect;

Route::get('logout', function()
{
    return view('pages.logout');
})->middleware(LogoutRedirect::class);

And logout.blade.php looks like

{{ Auth::logout() }}

I get the error (when trying to access localhost:8080/logout)

Use of undefined constant pages - assumed 'pages'

What could I do about it?


EDIT


I tried another approach (but with no better results):

  1. renamed the route which redirects to '/' to 'home'
  2. made a LogoutController in app/http/Controllers/Auth

    namespace App\Http\Controllers;
    use [...]
    class LogoutController extends Controller
    {
        public function logout() {
            Auth::logout();
            return Redirect::route('home');
    }
    }
    
  3. made the route

    Route::post('logout', array(
        'as' => 'account-sign-out',
        'uses' => 'Auth\LogoutController@logout'
    ));
    

The error I get is

  MethodNotAllowedHttpException in RouteCollection.php line 233:

That's the same error I get when I try to use the default logout defined in auth


Solution

  • You are trying to access the logout page with GET. But this doesn't work because your logout route is a post route. Change

    Route::post('logout', array(
        'as' => 'account-sign-out',
        'uses' => 'Auth\LogoutController@logout'
    ));
    

    by

    Route::get('logout', [
        'as' => 'account-sign-out',
        'uses' => 'Auth\LogoutController@logout'
    ]);
    

    When you go to the /logout route with the method GET(The default when you go to a page) it should work.