質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.31%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

1回答

3250閲覧

Laravel9 マルチログイン実装。 adminのパスワードリセットが機能しない。

tkm0604

総合スコア555

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2022/07/10 03:19

編集2022/07/10 07:12

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テーブルを参照出来ていないのが原因だと思うのですが、どこを修正すればいいのか分かりません。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

NewPasswordControllerのstoreメソッド。パスワードのチェックを行う際はadminsテーブルを利用するのでPasswordファサードの後にbroker(‘admins’)を追加するのが抜けておりました。

投稿2022/07/10 09:36

tkm0604

総合スコア555

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.31%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問