Search code examples
laravelforgot-passwordlaravel-maillaravel-breeze

How to trigger password reset without a notification in Laravel Breeze?


Using Laravel 9 with Breeze I want when a user with a specific role has been created, he should receive a custom welcome mail with a link to a view, where he needs to set a password

How can I just trigger the password reset without the default forgot password mail, but get the token that I can use in my custom mail for the link?


Solution

  • Create a random string of 35 characters.

    Create an entry in the password_resets table comprising the user's email address, a hashed (bcrypt()) version of the token just created, and the current timestamp

    Send the user an email with a link leading to /reset-password/{$token}

    Example, assuming $user is the new user account;

    $token = Str::random(35);
    
    DB::table('password_resets')->insert([
        'email' => $user->email,
        'token' => bcrypt($token),
        'created_at' => now(), 
    ]);
    
    $user->notify(new WelcomeNewUser($token));
    
    

    and then in the notification class;

        public $token;
    
        public function __construct($token)
        {
            $this->token = $token;
        }
    
    //
    
        public function toMail($notifiable)
        {
            return (new MailMessage)
                        ->line('You have been invited to use our application')
                        ->action('Set a password', url(route('password.reset',['token'=>$this->token])))
                        ->line('Thank you for using our application!');
        }