php Laravel 5 Auth 注销不会破坏会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29621965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:29:11  来源:igfitidea点击:

Laravel 5 Auth Logout not destroying session

phplaravel

提问by seoppc

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 get redirected to url specified at $redirectAfterLogoutbut it does not destroy session so even after hitting logout button I am able to see dashboard.

我正在为我的新项目使用 Laravel5 Auth 系统,我可以使用注册和登录功能没有任何问题,但注销没有按预期工作,但是我被重定向到指定的 url$redirectAfterLogout但它不会破坏会话,所以即使在点击后注销按钮我可以看到仪表板。

Does laravel has some bug in Auth system, please suggest, thanks

Laravel 是否在 Auth 系统中存在一些错误,请提出建议,谢谢

回答by Amita

You have not provided any piece of code that you have used. However, the following code works:

您没有提供任何您使用过的代码。但是,以下代码有效:

public function getLogout(){
    Auth::logout();
    Session::flush();
    return Redirect::to('/');
}

The Session::flush();clears all the existing sessions.

Session::flush();清除所有现有会话。

回答by ken-mills

Using Laravel 5.2, I registered a listener, handled the logout event and called Session::flush as suggested above. Seemed to work pretty well. Hope this is helpful.

使用 Laravel 5.2,我注册了一个监听器,处理了注销事件并按照上面的建议调用了 Session::flush。似乎工作得很好。希望这是有帮助的。

EventServiceProvider.php

事件服务提供者.php

protected $listen = [
    'App\Events\SomeEvent' => [
        'App\Listeners\EventListener',
    ],
    'Illuminate\Auth\Events\Logout' => [
        'App\Listeners\ClearSessionAfterUserLogout'
    ],
]; 

ClearSessionAfterUserLogout.php

ClearSessionAfterUserLogout.php

public function handle(Logout $event)
{
    Session::flush();
}

回答by Frederick G. Sandalo

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.

function getLogout()永远达不到,因此,logout()方法永远不会触发。

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');

回答by dani24

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:

我的问题是,当我点击注销按钮时,在此之前我有一些尚未回答的 http 请求,所以即使用户注销,稍后在待处理请求的响应中它再次登录。下面是一个例子:

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

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

因此,删除那些未答复的请求对我有用。我希望这个答案有帮助:)

回答by Brett

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

我切换到数据库会话驱动程序并在我的注销操作中使用以下代码

$request->session()->getHandler()->destroy($request->session()->getId());

回答by Gajanan Tagadpalle

You can simply override the logout method in AuthController.php

您可以简单地覆盖 AuthController.php 中的注销方法

Here is code sample:

这是代码示例:

public function logout(){
        Session::flush();
        Auth::guard($this->getGuard())->logout();
        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
    }

回答by andrei040191

trait AuthenticatesUsers

trait AuthenticatesUsers

public function logout(Request $request)

change this

改变这个

$request->session()->regenerate();

to this

对此

$request->session()->regenerate(true);

回答by Slavik Okara

Auth()->logout();

For the newest versions.

对于最新版本。

回答by Margus Pala

In your case you are not probably reaching the logout() method. If you are using Laravel 5 builting auth mechanism then you will run AuthenticatesAndRegistersUsers trait getLogout() method which does $this->auth->logout();

在您的情况下,您可能没有达到 logout() 方法。如果您使用 Laravel 5 构建身份验证机制,那么您将运行 AuthenticatesAndRegistersUsers trait getLogout() 方法$this->auth->logout();

Find this code edit the method like below for debugging. If you see the string "Logging out" then you must be logged out. Ohterwise something is wrong with your routing and logout is just never executed.

找到此代码编辑如下方法进行调试。如果您看到字符串“Logging out”,那么您必须已注销。否则,您的路由出了点问题,并且永远不会执行注销。

/**
 * Log the user out of the application.
 *
 * @return \Illuminate\Http\Response
 */
public function getLogout()
{
    dd("Logging out");
    $this->auth->logout();

    return redirect('/');
}

回答by samlev

I've been fighting with this, and I've come to a solution.

我一直在与这个斗争,我已经找到了解决方案。

In short: The Laravel session reads and writes with middleware. It reads the stored session in at the start of the request, and writes any changes at the end of the request. If you make a redirect, then the current request never finishes, and the middleware write doesn't happen.

简而言之:Laravel 会话使用中间件进行读写。它在请求开始时读取存储的会话,并在请求结束时写入任何更改。如果您进行重定向,则当前请求永远不会完成,并且不会发生中间件写入。

So, how to fix this? Depending on your implementation... you should returnthe redirect command rather than calling it directly.

那么,如何解决这个问题?根据您的实现...您应该return重定向命令而不是直接调用它。

return redirect($redirectAfterLogout)