Memo

メモ > 技術 > フレームワーク: Laravel > 強制ログイン

■強制ログイン
Laravel Recipes日本語版 | ユーザーのIDを利用してログインする http://recipes.laravel.jp/recipe/77 authentication - Laravel - Auth multi guard loginUsingId is not working as expexted - Stack Overflow https://stackoverflow.com/questions/52154633/laravel-auth-multi-guard-loginusingid-is-not-working-as... /config/auth.php で以下のように設定されているものとする (標準のユーザとして user があり、追加のユーザとして developer があるものとする)
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'passport', 'provider' => 'users', ], 'user' => [ 'driver' => 'session', 'provider' => 'users', ], 'developer' => [ 'driver' => 'session', 'provider' => 'developers', ], 'developer-api' => [ 'driver' => 'token', 'provider' => 'developers', ], ],
■userで強制ログイン users テーブルに taro@example.com ユーザがいる場合、以下のコードで強制的に認証してログイン済みにできる Auth::attempt() でユーザ名とパスワードが正しいか確認できる $id には users テーブルの代理キーが入り、その代理キーを Auth::loginUsingId() に渡すとログイン状態になる …と思ったが、代理キーではなく「成功すれば1」が入っている。IDを得るにはメールアドレスをもとにデータベースを検索するくらいか 要検討
/* * 強制ログインのテスト */ Route::get('/enter', function () { $id = Auth::attempt(['email' => 'taro@example.com', 'password' => 'abcd1234']); if ($id) { Auth::loginUsingId($id); return 'OK'; } else { return 'NG'; } });
■developerで強制ログイン developers テーブルに developer@example.com ユーザがいる場合、以下のコードで強制的に認証してログイン済みにできる $id には developers テーブルの代理キーが入る …と思ったが、代理キーではなく「成功すれば1」が入っている。IDを得るにはメールアドレスをもとにデータベースを検索するくらいか 要検討
/* * 強制ログインのテスト */ Route::get('/enter', function () { $id = Auth::guard('developer')->attempt(['email' => 'developer@example.com', 'password' => 'abcd1234']); if ($id) { Auth::guard('developer')->loginUsingId($id); return 'OK'; } else { return 'NG'; } });

Advertisement