Laravel Socialiteでログイン後元の画面に戻るようにする
はじめに
Laravel Socialite っていうソーシャルログインする為のパッケージがあるんだけど、これの使い方とか載せてるサイト見ても、サンプルなのかログイン後 echo して終わってたり、 redirect()->to('/'); しかやってなくてよく分からなかった。
なので、通常のログインの時に使ってる Illuminate\Foundation\Auth\AuthenticatesUsers のコード読んでアクセスしようとしたページに戻す方法調べたのでメモ。
調査メモ
とりあえず、ログイン後のリダイレクトしてる所見ればええやろってことで、ファイル開いて redirect で検索してでてきたこのコードを読む。
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
すると redirect()->intended() とかいうメソッド叩いてたので、それが定義されてる Illuminate\Routing\Redirector.php を覗く。
public function intended($default = '/', $status = 302, $headers = [], $secure = null)
{
$path = $this->session->pull('url.intended', $default);
return $this->to($path, $status, $headers, $secure);
}
セッション使ってるっぽいしこれやな! ってことで、Twitter ログイン後のコールバック処理の中で↓のようなコード書いて無事アクセスしようとしたページに戻れるようになった。
return redirect()->intended('/');
ちゃんちゃん。