Laravel Redirect does not work in Event handler / listener

后端 未结 2 593
旧时难觅i
旧时难觅i 2021-01-19 18:35

I have a Auth.Attempt event handler class, which I detect user\'s login attempts to decide to lock user\'s account. However, when I tried to redirect user to login page with

2条回答
  •  逝去的感伤
    2021-01-19 19:21

    Not getting to much into this very interesting discussion:

    Should exceptions be used for flow control

    You can try setting up your own exception handler and redirect from there on to the login page.

    class FancyException extends Exception {}
    
    App::error(function(FancyException $e, $code, $fromConsole)
    {
        $msg = $e->getMessage();        
        Log::error($msg);
    
        if ( $fromConsole )
        {
            return 'Error '.$code.': '.$msg."\n";
        }
    
        if (Config::get('app.debug') == false) {
            return Redirect::route('your.login.route');
        }
        else
        {
            //some debug stuff here
        }
    
    
    });
    

    And in your function:

    public function onLoginAttempt($data)
    {
        //detection process.......
        // if login attempts more than max attempts
        throw new FancyException("some msg here");
    }
    

提交回复
热议问题