KenPrz / HomeSphere

1 stars 0 forks source link

Add Email Notifications for Password Updates and Two-Factor Authentication #11

Open KenPrz opened 1 year ago

KenPrz commented 1 year ago

Background: In order to enhance the security and user experience of our Laravel application, we need to implement email notifications for password updates and two-factor authentication (2FA) processes. Email notifications will provide users with important information about changes to their account security, helping them stay informed and ensuring they have control over their account.

Objective: The goal of this feature is to improve account security and user trust by implementing the following functionalities:

  1. Password Updates Email Notification: When a user changes their password, the system should automatically send an email notification to the user's registered email address. This email should confirm that the password has been successfully updated. In case the password change was unauthorized, the email should provide a way for users to take action.

  2. Two-Factor Authentication (2FA) Email Notification: Whenever a user enables or disables two-factor authentication for their account, an email notification should be sent. This email should contain information about the change and include steps to follow if the change was not authorized.

KenPrz commented 1 year ago

Simple implementation guide for adding email notifications for password updates and two-factor authentication (2FA) in a Laravel application:

Step 1: Configure Mail Settings

  1. Open the .env file and set up your mail configuration, including the mail driver, host, port, username, and password.
  2. Use a mail service like Mailgun, SendGrid, or SMTP for reliable email delivery.

Step 2: Create Email Templates

  1. Create Blade templates for password update and 2FA notifications in the resources/views/emails directory.
  2. Customize the templates with relevant content, placeholders, and links.

Step 3: Implement Email Sending Logic

  1. In your controller or service, use the Mail facade to send emails.
  2. For password updates, send an email after successfully updating the password.
  3. For 2FA changes, trigger an email when enabling or disabling 2FA.

Example Code for Sending Emails:

use Illuminate\Support\Facades\Mail;
use App\Mail\PasswordUpdateNotification;
use App\Mail\TwoFactorAuthNotification;

// Inside your controller or service method
$user = auth()->user();

// Send password update notification
Mail::to($user->email)->send(new PasswordUpdateNotification($user));

// Send 2FA notification
Mail::to($user->email)->send(new TwoFactorAuthNotification($user));

Step 4: Create Mailables

  1. Create mailable classes for each type of email notification using the php artisan make:mail command.
  2. Customize the mailables to use the appropriate email template and provide necessary data.

Example Mailable Code:

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class PasswordUpdateNotification extends Mailable
{
    use Queueable, SerializesModels;

    public $user;

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

    public function build()
    {
        return $this->view('emails.password_update_notification')
            ->subject('Password Update Notification');
    }
}

Step 5: Add Email Links and Instructions

  1. In your email templates, include relevant links to the application for users to verify changes.
  2. Provide clear instructions on what users should do if they did not initiate the change.

Step 6: Update User Settings (Optional)

  1. If you want users to control whether they receive these notifications, update the user settings in your database.
  2. Provide a UI in your application for users to enable or disable these notifications.

Step 7: Testing

  1. Test the email notifications thoroughly using different email providers.
  2. Test both successful scenarios and failure cases (e.g., emails not sending, marked as spam).
  3. Verify the accuracy of links and instructions in the emails.