繁体   English   中英

防止注销身份验证laravel

[英]Prevent logout auth laravel

有什么方法可以监听注销事件并做出诸如在auth laravel上重定向的决策? 我知道有一些登录/注销侦听器,但重定向不起作用:

class LogSuccessfulLogout
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Logout  $event
     * @return void
     */
   public function handle(Logout $event)
   {
    if($event->user) {


        $new = Auth::user()->cars()->where('status', 1)->count();
        $inProgress = Auth::user()->cars()->where('status', 2)->count();

        if($new > 0 || $inProgress > 0){
            redirect('/');
        }
    }

  }
}

这是默认Laravel 5.2 AuthController中调用的默认注销功能

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

因此,您可以在AuthController中使用以下代码来设置redirectAfterLogout属性来更改重定向网址,

private $redirectAfterLogout = '/new-logout-redirect';

或者您可以选择简单地覆盖注销功能。 重定向无法直接在您的监听器上运行。

这是修改后的登出函数,带有您可以在AuthController中放置的逻辑,

public function logout()
{
    Auth::guard($this->getGuard())->logout();

    $new = Auth::user()->cars()->where('status', 1)->count();
    $inProgress = Auth::user()->cars()->where('status', 2)->count();

    if($new > 0 || $inProgress > 0){
        return redirect('/');
    }

    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM