2017-12-05 12 views
2

Laravel(v5.5)による組み込み認証機能の実際のログイン機能に関して、私の問題、またはむしろ疑問です。Laravel 5.5 - ログインの説明?

ユーザをシステムにログインさせることに関して、どこで魔法が発生するのか分かりません。ここで

LoginController

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

     if ($this->hasTooManyLoginAttempts($request)) { 
      $this->fireLockoutEvent($request); 

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

     if ($this->attemptLogin($request)) { 
      return $this->sendLoginResponse($request); 
     } 


     $this->incrementLoginAttempts($request); 

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

、ここでattemptedLogin方法butthis 1はまだ私に任意の閉鎖を与えるものではありません内のログイン方法。

protected function attemptLogin(Request $request) 
{ 
    return $this->guard()->attempt(
     $this->credentials($request), $request->filled('remember') 
    ); 
} 

ユーザーは、ユーザーがアプリケーションにログインした正確な場所を私に説明できますか?私はドキュメントからそれを読むこともできません。 私はあまりにも盲目的であるか愚かですが、私は本当にそれを見ません。

ありがとうございます! /src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

+4

もあなたが正しい道を開始しているために十分にスマートです。ウサギの穴を通って、商品は 'guard() - > attempt()'の中に入っています。 – castis

+0

@castisは正しいアイデアを持っています。 [こちらをご覧ください](https://github.com/laravel/framework/blob/5.5/src/Illuminate/Auth/Middleware/Authenticate.php)補足として、OPには腹を立てる権利があります。コードに従うのは本当に難しいです。どこでもマジックメソッド。 – Andrew

+0

また、特性AuthenticatesUsersを見てください – IseNgaRt

答えて

0

はい、あなたの勘違いは正しいです。実際のログイン(資格情報と一致し、成功した場合は、ユーザーをログイン)はattemptLoginメソッドで行われます。ただ、コメントとコードを読んで、完全にコードを理解するためにLaravel docs

で説明したように、それは単にAuth::attemptコールと同じです。私はAuthenticatesUsersThrottlesLoginsの形質から下のコードを抜き出しました。 コメントはかなり自明です:

/** 
* Handle a login request to the application. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse 
*/ 
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 ($this->hasTooManyLoginAttempts($request)) { 
     $this->fireLockoutEvent($request); 
     return $this->sendLockoutResponse($request); 
    } 
    if ($this->attemptLogin($request)) { 
     return $this->sendLoginResponse($request); 
    } 
    // 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. 
    $this->incrementLoginAttempts($request); 
    return $this->sendFailedLoginResponse($request); 
} 
/** 
* Validate the user login request. 
* 
* @param \Illuminate\Http\Request $request 
* @return void 
*/ 
protected function validateLogin(Request $request) 
{ 
    $this->validate($request, [ 
     $this->username() => 'required|string', 
     'password' => 'required|string', 
    ]); 
} 
/** 
* Attempt to log the user into the application. 
* 
* @param \Illuminate\Http\Request $request 
* @return bool 
*/ 
protected function attemptLogin(Request $request) 
{ 
    return $this->guard()->attempt(
     $this->credentials($request), $request->filled('remember') 
    ); 
} 
/** 
* Send the response after the user was authenticated. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\Response 
*/ 
protected function sendLoginResponse(Request $request) 
{ 
    $request->session()->regenerate(); 
    $this->clearLoginAttempts($request); 
    return $this->authenticated($request, $this->guard()->user()) 
      ?: redirect()->intended($this->redirectPath()); 
} 
/** 
* The user has been authenticated. 
* 
* @param \Illuminate\Http\Request $request 
* @param mixed $user 
* @return mixed 
*/ 
protected function authenticated(Request $request, $user) 
{ 
    // 
} 
/** 
* Get the failed login response instance. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Symfony\Component\HttpFoundation\Response 
* 
* @throws ValidationException 
*/ 
protected function sendFailedLoginResponse(Request $request) 
{ 
    throw ValidationException::withMessages([ 
     $this->username() => [trans('auth.failed')], 
    ]); 
} 
/** 
* Determine if the user has too many failed login attempts. 
* 
* @param \Illuminate\Http\Request $request 
* @return bool 
*/ 
protected function hasTooManyLoginAttempts(Request $request) 
{ 
    return $this->limiter()->tooManyAttempts(
     $this->throttleKey($request), $this->maxAttempts(), $this->decayMinutes() 
    ); 
} 
/** 
* Increment the login attempts for the user. 
* 
* @param \Illuminate\Http\Request $request 
* @return void 
*/ 
protected function incrementLoginAttempts(Request $request) 
{ 
    $this->limiter()->hit(
     $this->throttleKey($request), $this->decayMinutes() 
    ); 
} 
/** 
* Redirect the user after determining they are locked out. 
* 
* @param \Illuminate\Http\Request $request 
* @return void 
* @throws \Illuminate\Validation\ValidationException 
*/ 
protected function sendLockoutResponse(Request $request) 
{ 
    $seconds = $this->limiter()->availableIn(
     $this->throttleKey($request) 
    ); 
    throw ValidationException::withMessages([ 
     $this->username() => [Lang::get('auth.throttle', ['seconds' => $seconds])], 
    ])->status(423); 
} 
/** 
* Clear the login locks for the given user credentials. 
* 
* @param \Illuminate\Http\Request $request 
* @return void 
*/ 
protected function clearLoginAttempts(Request $request) 
{ 
    $this->limiter()->clear($this->throttleKey($request)); 
} 
/** 
* Fire an event when a lockout occurs. 
* 
* @param \Illuminate\Http\Request $request 
* @return void 
*/ 
protected function fireLockoutEvent(Request $request) 
{ 
    event(new Lockout($request)); 
} 
/** 
* Get the throttle key for the given request. 
* 
* @param \Illuminate\Http\Request $request 
* @return string 
*/ 
protected function throttleKey(Request $request) 
{ 
    return Str::lower($request->input($this->username())).'|'.$request->ip(); 
} 
/** 
* Get the rate limiter instance. 
* 
* @return \Illuminate\Cache\RateLimiter 
*/ 
protected function limiter() 
{ 
    return app(RateLimiter::class); 
} 
0
protected function sendLoginResponse(Request $request) 
{ 
    $request->session()->regenerate(); 
    $this->clearLoginAttempts($request); 
    return $this->authenticated($request, $this->guard()->user()) 
      ?: redirect()->intended($this->redirectPath()); 
} 

ユーザーが最終的にセッションに記録されます場所です。

正しいユーザーIDを使用してAuth::loginUsingId($userId)を追加するだけで、ユーザーにログインすることもできます。ので、多分、私は何とかそれを少し:)

ユーザーが認証される方法を明確にすることができるようになります、私はほとんど同じ質問に対する答えを見つけようと最後の数日間を過ごしてきた

0

(ザッツ安全ではありません)特定のモデルに対してどのガードを使用しているかによって異なります。あなたが既に知っているとしてだけで、attemptLogin方法がガード()メソッドの結果に呼び出された

は:

protected function attemptLogin(Request $request) 
{ 
    return $this->guard()->attempt(
     $this->credentials($request), $request->filled('remember') 
    ); 
} 

あなたはデフォルトの 'セッションのガードを使用している場合、このメソッドはIlluminate\Auth\SessionGuard.phpクラスのインスタンスを返します。 。

このクラスは、attemptメソッドを定義します。このメソッドは、ユーザー資格情報が有効な場合はloginメソッドを呼び出します。これはユーザーが実際に認証される最終的な場所のようです。