2013-07-22 1 views
3

사용자의 계정을 잠그기로 결정한 사용자의 로그인 시도를 감지하는 Auth.Attempt 이벤트 처리기 클래스가 있습니다. 그러나 플래시 메시지가있는 로그인 페이지로 사용자를 리디렉션하려고 시도했을 때 리디렉션이 작동하지 않는다는 것을 알았지 만 다음 단계로 진행됩니다. 이벤트의 프로세스를 중단하고 사용자 지정 경고 메시지를 보내려고합니다. 누구든지 나를 도울 수 있습니까? 고마워.Laravel Redirect가 이벤트 처리기/수신기에서 작동하지 않습니다.

내 이벤트 핸들러 :

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

지금 내가이 일을하고있는 방법은 다음과 같습니다가 :

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

그것은 작동하지만 난 그것을 할 수있는 완벽한 방법이 있는지 모르겠습니다.

+0

왜'리디렉션을 사용합니다'? –

+0

인증을 처리하고 싶지 않기 때문에. 사용자 이름과 암호의 유효성을 검사하기 전에 사용자 로그인 시도를 확인하려고합니다. 사용자의 계정이 잠겨 있거나 IP가 차단 된 경우 프로세스가 종료되고 플래시 메시지가있는 홈 페이지로 리디렉션됩니다. – Jonathan

+0

아니요,'리디렉션 사용 '이 유효한 네임 스페이스입니까? –

답변

3

이 매우 흥미로운 토론에 많은에 도착하지 :

Should exceptions be used for flow control

당신은 당신의 자신의 예외 핸들러를 설정하는 시도하고 로그인 페이지에 거기에서 리디렉션 할 수 있습니다.

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"); 
} 
+0

나는 예외 처리기 접근법을 사용했으며 훌륭하게 작동합니다! 이 우수한 패턴을 지적 해 주셔서 감사합니다. – CyberMonk

+0

감사합니다.이 도움이되었습니다. –

관련 문제