Laravel Redirect not working in event handler / listener

I have an Auth.Attempt event handler class that I found that user login attempts have decided to lock the user account. However, when I tried to redirect the user to the login page with the flash message, I found that the redirect did not work, it still continues the next step. I want to interrupt the process in the event and provide my own warning message. Can someone help me? Many thanks.

My event handler:

namespace MyApp\Handlers\Security;

use DB;
use Session;
use Redirect;

class LoginHandler 
{
    /**
     * Maximum attempts
     * If user tries to login but failed more than this number, User account will be locked
     * 
     * @var integer
     */
    private $max_attemtps;

    /**
     * Maximum attempts per IP
     * If an IP / Device tries to login but failed more than this number, the IP will be blocked
     * 
     * @var integer
     */
    private $ip_max_attempts;

    public function __construct()
    {
        $this->max_attempts = 10;
        $this->ip_max_attempts = 5;
    }

    public function onLoginAttempt($data)
    {
        //detection process.......
        // if login attempts more than max attempts
        return Redirect::to('/')->with('message', 'Your account has been locked.');
    }
}

Now, the way I do this is as follows:

Session::flash('message', 'Your account has been locked.');
header('Location: '.URL::to('/'));

This works, but I'm not sure if this is the perfect way to do this.

+4
2

:

>

.

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
    }


});

:

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

HttpException, . ,

abort(redirect('/'));
0

Source: https://habr.com/ru/post/1664985/


All Articles