Summer Sale! All accounts are 50% off this week.

liquidonthedrums's avatar

Password Reset From Table Action

Can anyone provide some guidance on how to create an action to allow an admin to initiate a password reset for a user (ie. create token and send email). I have set up the user resource and have added an action to the last column of the user list. I'm just not sure what i need to do from there to get this happening. Any assistance would be appreciated. Thx

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! To create an action that allows an admin to initiate a password reset for a user, you can follow these steps. I'll assume you're using Laravel for this example.

  1. Create a Custom Action: First, you need to create a custom action for your user resource. This action will generate a password reset token and send an email to the user.

  2. Generate Password Reset Token: Use Laravel's built-in password reset functionality to generate a token.

  3. Send Password Reset Email: Use Laravel's notification system to send the password reset email.

Here's a step-by-step guide:

Step 1: Create the Action

Create a new action using Laravel's Artisan command:

php artisan make:action SendPasswordReset

Step 2: Implement the Action

Edit the generated action file app/Actions/SendPasswordReset.php to include the logic for generating the token and sending the email.

namespace App\Actions;

use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Notification;
use App\Notifications\PasswordResetNotification;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\Text;

class SendPasswordReset extends Action
{
    public $name = 'Send Password Reset';

    public function handle(ActionFields $fields, \Illuminate\Support\Collection $models)
    {
        foreach ($models as $user) {
            // Generate the password reset token
            $token = Password::broker()->createToken($user);

            // Send the password reset email
            Notification::send($user, new PasswordResetNotification($token));
        }

        return Action::message('Password reset email sent!');
    }

    public function fields()
    {
        return [];
    }
}

Step 3: Create the Notification

Create a new notification for the password reset email:

php artisan make:notification PasswordResetNotification

Edit the generated notification file app/Notifications/PasswordResetNotification.php:

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class PasswordResetNotification extends Notification
{
    use Queueable;

    protected $token;

    public function __construct($token)
    {
        $this->token = $token;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('You are receiving this email because we received a password reset request for your account.')
                    ->action('Reset Password', url(config('app.url').route('password.reset', $this->token, false)))
                    ->line('If you did not request a password reset, no further action is required.');
    }
}

Step 4: Register the Action in Your User Resource

Edit your User resource file to include the action:

namespace App\Nova;

use App\Actions\SendPasswordReset;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Email;
use Laravel\Nova\Http\Requests\NovaRequest;

class User extends Resource
{
    public static $model = 'App\User';

    public function fields(NovaRequest $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Name'),
            Email::make('Email'),
        ];
    }

    public function actions(NovaRequest $request)
    {
        return [
            new SendPasswordReset,
        ];
    }
}

Step 5: Test the Action

Now, when you go to your user list in Nova, you should see the "Send Password Reset" action available. When you select a user and run this action, it will generate a password reset token and send the email to the user.

That's it! You now have a custom action that allows an admin to initiate a password reset for a user.

Please or to participate in this conversation.