2016-09-04 5 views
2

안녕하세요. ban_status가있는 로그인을 시도하는 사용자를 리디렉션하려고합니다. = 0Laravel 5.3 로그인 할 때 금지 된 사용자를 리디렉션

나는 이것을 수행하는 가장 좋은 방법이 무엇인지 궁금하여, 그냥 로그 아웃하고 금지 된 페이지로 리디렉션하려고 생각했지만 금지 된 사용자 만이 페이지에 액세스하거나 양식은 '금지되어 있습니다'등등. 내 코드는 현재 다음과 같습니다.

public function login (Request $ request) { $ this-> validateLogin ($ request);

// If the class is using the ThrottlesLogins trait, we can automatically throttle 
    // the login attempts for this application. We'll key this by the username and 
    // the IP address of the client making these requests into this application. 
    if ($lockedOut = $this->hasTooManyLoginAttempts($request)) { 
     $this->fireLockoutEvent($request); 

     return $this->sendLockoutResponse($request); 
    } 

    $credentials = $this->credentials($request); 

    if ($this->guard()->attempt($credentials, $request->has('remember'))) { 
     if ($this->guard()->user()->ban_status === 0) { // Check if the user is banned or not before allowing login 
      return $this->sendLoginResponse($request); 
     } 
     else { 

      $this->guard()->logout(); 

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

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

      return redirect('/banned'); // If the user banned_status is set to 1, then they will be redirected to a banned page. 
     } 
    } 

    // If the login attempt was unsuccessful we will increment the number of attempts 
    // to login and redirect the user back to the login form. Of course, when this 
    // user surpasses their maximum number of attempts they will get locked out. 
    if (! $lockedOut) { 
     $this->incrementLoginAttempts($request); 
    } 

    return $this->sendFailedLoginResponse($request); 
} 
+0

당신은 아마 사용자가 금지되는 경우 확인하고 만약 그렇다면, 그것은 적절한 페이지로 리디렉션 미들웨어를 작성할 수 있습니다. – Skysplit

답변

3

이 방법을 사용하면 경로 이름을 알고있는 사용자가 다른 경로에 직접 액세스하지 못하게됩니다.

미들웨어를 추가하고 다른 모든 경로를 보호하고 미들웨어의 유효성을 검사하지 못하면 차단 된 페이지로 사용자를 리디렉션하면됩니다.

는 여기를 참조하십시오 : https://laravel.com/docs/5.3/middleware

+0

이 문제를 해결하려면 @if (Auth :: check() && Auth :: user() -> ban_status! = 0) {{Auth :: logout()}} endif 페이지? – user6073700

+0

그것은 많은 중복 코드입니다. 미들웨어를 추가하면 몇 줄 밖에 없으므로 모든 것을 제거 할 수 있습니다 ... – pseudoanime

+0

여분의 조건을 확인하기 위해 인증 미들웨어를 수정하는 것이 더 쉬울까요? – user6073700

관련 문제