Laravel 6.xの認証・ログインの中身
toCとtoBでログイン後の遷移先を分けたいと思って調べたこと
LoginControllerの中身
ルートに書かれているアクションは全てトレイトAuthenticatesUsersにある。
public function showLoginForm()
{
return view('auth.login');
}
public function login(Request $request)
{
$this->validateLogin($request);// PW等バリデーション
// ThrottlesLoginsトレイトによりユーザ名とIPからログイン試行を制限できる
if (method_exists($this, 'hasTooManyLoginAttempts') &&
$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);
}
protected function attemptLogin(Request $request)
{
return $this->guard()->attempt(
$this->credentials($request), $request->filled('remember')
);
}
attemptLoginの返り値=attempt()の返り値。
このguard()は何なのか。
use Illuminate\Support\Facades\Auth;
/**
* Get the guard to be used during authentication.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return Auth::guard();
}
Illuminate\Support\Facades\Auth
@method static mixed guard(string|null $name = null)
Illuminate\Contracts\Auth\Factory
/**
* Get a guard instance by name.
*
* @param string|null $name
* @return mixed
*/
public function guard($name = null);
/**
* Attempt to authenticate a user using the given credentials.
*
* @param array $credentials
* @param bool $remember
* @return bool
*/
public function attempt(array $credentials = [], $remember = false);
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
return $request->only($this->username(), 'password');
}
処理が何にもないfunction function_name () ;
これが返すのはtrueなのかfalseなのか???
return を省略した場合は NULL を返します。
https://www.php.net/manual/ja/functions.returning-values.php
結局分からず、下記サイトがとても分かりやすかった。
https://reffect.co.jp/laravel/laravel-authentification-by-code-base#attemptLogin
attempt() は、SessionGuardインスタンスのattemptメソッドらしい。