Laravel 5 Auth Logout not destroying session

前端 未结 12 1588
忘掉有多难
忘掉有多难 2021-01-04 00:10

I am using Laravel5 Auth system for my new project, I am able to use registration and login functions with out any problem but logout is not working as expected, however I g

相关标签:
12条回答
  • 2021-01-04 00:21

    I had the same issue and I tried everything, but in the end I could fix it.

    My problem was that when I hit on the logout button, before that I had some http requests that weren't answered yet, so even when the user was log out, later with the response of the pending requests it got logged in again. Here is an example:

    Another Request |   ***********************************
    Logout Request  |          ********************
                    |
    Time            | --|------|-------------------|------|------>
                       t1      t2                  t3     t4
    

    So Removing those non-answered requests worked for me. I hope that this answer helps :)

    0 讨论(0)
  • 2021-01-04 00:21

    By accepting the request object in a controller action (Remember to add this after the controller namespace declaration: use Auth; ):

     /**
     *
     * Render page
     *
     * @route POST /user/{user_id}/logout
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function logout(Request $request) {
        Auth::logout();
        $request->session()->flush();
    }
    
    0 讨论(0)
  • 2021-01-04 00:21

    I switched to the database session driver and used the following code in my logout action

    $request->session()->getHandler()->destroy($request->session()->getId());
    
    0 讨论(0)
  • 2021-01-04 00:25
    Auth()->logout();
    

    For the newest versions.

    0 讨论(0)
  • 2021-01-04 00:27

    You can simply override the logout method in AuthController.php

    Here is code sample:

    public function logout(){
            Session::flush();
            Auth::guard($this->getGuard())->logout();
            return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
        }
    
    0 讨论(0)
  • 2021-01-04 00:28

    It seems that in the

    /vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php 
    

    The function getLogout() is never reached, hence the logout() method never fires.

    In my case, in my

    /app/Http/routes.php
    

    Iinstead of this:

    Route::get('auth/logout', 'Auth\AuthController@getLogout');  
    

    I changed it to:

    Route::get('auth/logout', 'Auth\AuthController@logout');
    
    0 讨论(0)
提交回复
热议问题