Laravel9 Breezeを用いて、デフォルトのUserテーブルに加えて、Adminテーブルでのマルチログイン機能を実装しました。
Adminアカウントでの登録、ログインは動作しています。
しかし、Adminユーザーでのパスワードリセット(localhost/admin/reset-password/)を行うと、
「このメールアドレスに一致するユーザーがいません。」
となります。
以下の手順で、Adminユーザーでのパスワードリセットを実装しました。
1.Admin登録mailアドレスにリセットメールが送信されるように、PasswordResetLinkController.phpファイルのPasswordファサードの後にbrokerメソッドを追加
PHP
1PasswordResetLinkController.php 2 3 4 public function store(Request $request) 5 { 6 $request->validate([ 7 'email' => ['required', 'email'], 8 ]); 9 10 // We will send the password reset link to this user. Once we have attempted 11 // to send the link, we will examine the response then see the message we 12 // need to show to the user. Finally, we'll send out a proper response. 13 14 $status = Password::sendResetLink( 15 $request->only('email') 16 ); 17//以下のコードを追加 18 $status = Password::broker('admins')->sendResetLink( 19 $request->only('email') 20 ); 21//追加ここまで 22 23 return Password::RESET_LINK_SENT == $status 24 ? back()->with('status', __($status)) 25 : back()->withInput($request->only('email')) 26 ->withErrors(['email' => __($status)]); 27 }
上記でadminアカウントのリセットメールが受信されるようになりました。
2.vender/laravel/framework/src/illminate/Notifications/「ResetPassword.php」の内容をコピーして、新たにAdminResetPassword.phpファイルをResetPassword.phpと同じ階層に作成
ResetPassword.phpファイルをコピーしたAdminResetPassword.phpで、以下の箇所を変更しました。
PHP
1AdminResetPassword.php 2 3// class ResetPassword extends Notification 4class AdminResetPassword extends Notification 5 6 7//function buildMailMessage($url)、 function resetUrl($notifiable)を編集 8 9 protected function buildMailMessage($url) 10 { 11 return (new MailMessage) 12 ->subject(Lang::get('Admin Reset Password Notification')) 13 ->line(Lang::get('You are receiving this email because we received a password reset request for your account.')) 14 ->action(Lang::get('Reset Password'), $url) 15 ->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.admins'.config('auth.defaults.passwords').'.expire')])) 16 ->line(Lang::get('If you did not request a password reset, no further action is required.')); 17 } 18 19 20 protected function resetUrl($notifiable) 21 { 22 if (static::$createUrlCallback) { 23 return call_user_func(static::$createUrlCallback, $notifiable, $this->token); 24 } 25 26 return url(route('admin.password.reset', [ 27 'token' => $this->token, 28 'email' => $notifiable->getEmailForPasswordReset(), 29 ], false)); 30 }
3.app/Moldes/Admin.phpファイルにsendPasswordResetNotificationメソッドを追加
PHP
1Admin.php 2 3namespace App\Models; 4 5use Illuminate\Contracts\Auth\MustVerifyEmail; 6use Illuminate\Database\Eloquent\Factories\HasFactory; 7use Illuminate\Foundation\Auth\User as Authenticatable; 8use Illuminate\Notifications\Notifiable; 9use Laravel\Sanctum\HasApiTokens; 10use Illuminate\Auth\Notifications\AdminResetPassword as ResetPasswordNotification; 11 12class Admin extends Authenticatable implements MustVerifyEmail 13{ 14 use HasApiTokens; 15 use HasFactory; 16 use Notifiable; 17 18 public function sendPasswordResetNotification($token){ 19 20 $this->notify(new ResetPasswordNotification($token)); 21 } 22
パスワードリセット(http://localhost/admin/forgot-password)からパスワード再設定用のURLをメールに送信すると、「Admin Reset Password Notification」というタイトルでパスワードリセットのリンク付きメッセージが受信されます。
Admin Reset Password Notificationメール本文の「パスワード再設定」ボタンをクリックすると、localhost/admin/reset-password/へ遷移します。
localhost/admin/reset-password/でパスワードを再設定すると、「このメールアドレスに一致するユーザーがいません。」となり、adminユーザのパスワード再設定が完了しません。
パスワードリセットに関しては以下の記事を参照しました。
https://reffect.co.jp/laravel/breeze_multi_auth#i-19
※記事はLaravel8で説明しています。
パスワード再設定の際にlaravelがadminテーブルを参照出来ていないのが原因だと思うのですが、どこを修正すればいいのか分かりません。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。