振り返りが追いついておらぬ
リセットパスワード用URLリンクをメールで通知
通知をQueue?に詰める
詰めるのはdoc確認するだけで簡単でした。
<?php use Illuminate\Support\Facades\Password; // 省略 $status = Password::sendResetLink([ 'email' => $email, ]);
$status
は下記の定数が返ってきます。
それにあわせて処理やResponseをどう返すかなど考えるといいかも。
ログインのユーザータイプ?が複数ある場合
一般用、管理用とかで分けないといけない場合、 auth.php
のproviderとpasswordsで対応するモデル、テーブルなどを設定します。
<?php // 省略 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'table' => 'password_resets', 'expire' => 60, 'throttle' => 60, ], 'admins' => [ 'provider' => 'admins', 'table' => 'admin_password_resets', 'expire' => 60, 'throttle' => 60, ], ],
そうすると、 broker
で指定することができるので下記のようにできます。
<?php // 省略 $status = Password::broker('admin')->sendResetLink([ 'email' => $email, ]);
通知後の処理
通常はデフォルトの機能だけで対応できますが、メールの内容変更したいとかあれば、カスタマイズできるように組み立てる必要があります。
なんとModelで関数をオーバーライドします。最初なんでモデルなんだ・・・?とか混乱してたけど、ユーザーはモデルだよななるほどなってなった。
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use App\Notifications\ResetPassword; class User extends Authenticatable { use Notifiable; public function sendPasswordResetNotification($token) { $this->notify(new ResetPassword($token)); } }
App\Notifications\ResetPassword
を実装する必要があります。
こっちはこんな感じでおk
<?php namespace App\Notifications; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification; class ResetPassword extends ResetPasswordNotification { /** * @param mixed $notifiable * @return \Illuminate\Notifications\Messages\MailMessage */ public function toMail(mixed $notifiable): MailMessage { $url = "..."; return (new MailMessage()) ->line('The introduction to the notification.') ->action('Notification Action', $url) ->line('Thank you for using our application!.'); }
参考URL
ロガー
普通に使えて便利だし、すでに拡張性高いし、もしもっと拡張したいってなったらフォーマット変更する方法もあるし、ららべるすごい
<?php logger()->info('Action', [ 'RequestClass' => get_class($this), 'context' => [ 'hoge' => $hoge, 'status' => $status, ], ]);
これでJsonがでてくる。そのままElasticSearchとかに投げれる(多分)