How to Create Laravel Custom Forgot & Reset Password Example

In this Laravel tutorial, we will discuss how to customize Laravel’s built-in authentication system, which simplifies the process of implementing user authentication. We will create a custom Forgot password and rest feature in the Laravel app. We will demonstrate how to create a custom forgot and reset password example using Laravel. The process of creating…

By.

•

min read

In this Laravel tutorial, we will discuss how to customize Laravel’s built-in authentication system, which simplifies the process of implementing user authentication. We will create a custom Forgot password and rest feature in the Laravel app.

We will demonstrate how to create a custom forgot and reset password example using Laravel. The process of creating a custom forgot and reset password system in Laravel involves setting up a new Laravel project, configuring the database, running the authentication scaffolding, customizing routes, modifying email templates, creating custom notifications, customizing reset password logic, and testing the functionality.

Let’s get started and achieve this by following an easy step-by-step process.

 

How to Create a Forgot and Reset Password App?

Follow these steps to create a customer Forgot and Reset Password Layout with Database configuration:

Step 1: Create New Laravel Project
Step 2: Set Up Database Configuration
Step 3: Run Auth Scaffolding
Step 4: Customize Routes
Step 5: Modify Email Templates
Step 6: Create a Custom Notification
Step 7: Customize Reset Password Logic
Step 8: Test the Functionality

 

Step 1: Create New Laravel Project

Begin by creating a new Laravel project using the command:

composer create-project --prefer-dist laravel/laravel custom-reset-password

 

Step 2: Set Up Database Configuration

Next, set up the database by updating the .env file with the correct database credentials:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

 

Step 3: Run Auth Scaffolding

Now, run the Laravel authentication scaffolding command:

composer require laravel/ui
php artisan ui bootstrap --auth
npm install
npm run dev

 

Step 4: Customize Routes

Navigate to routes/web.php and modify the routes for the custom reset password functionality:

// Import the required classes at the top of the file
use App\Http\Controllers\CustomResetPasswordController;

// Add the following routes
Route::get('password/reset/{token}', [CustomResetPasswordController::class, 'showResetForm'])->name('password.reset');
Route::post('password/reset', [CustomResetPasswordController::class, 'reset'])->name('password.update');

 

Step 5: Modify Email Templates

In the resources/views/emails directory, create a new file named password-reset.blade.php and customize the email template as needed.

 

Step 6: Create Custom Notification

Create a new file named CustomResetPasswordNotification.php in the app\Notifications directory and customize the notification class:

// Import the required classes at the top of the file
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class CustomResetPasswordNotification extends Notification
{
    public $token;

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

    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 7: Customize Reset Password Logic

Create a new controller named CustomResetPasswordController.php in the app/Http/Controllers directory and extend the default ResetPasswordController:

// Import the required classes at the top of the file
use App\Notifications\CustomResetPasswordNotification;
use Illuminate\Foundation\Auth\ResetsPasswords;

class CustomResetPasswordController extends Controller
{
    use ResetsPasswords;

    // Override the sendResetLinkEmail method
    protected function sendResetLinkEmail(Request $request)
    {
        $this->validateEmail($request);

        // We will send the password reset link to this user.
        $user = $this->broker()->getUser($this->credentials($request));

        if ($user && ! $user->isPasswordResetDisabled()) {
            $user->sendPasswordResetNotification(
                $this->broker()->createToken($user)
            );
        }

        return $this->sendResetLinkResponse($request, Password::RESET_LINK_SENT);
    }
}

 

Next, update the User model to use the custom notification:

// Import the required class at the top of the file
use App\Notifications\CustomResetPasswordNotification;

class User extends Authenticatable
{
    // ...

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new CustomResetPasswordNotification($token));
    }
}

 

Step 8: Test the Functionality

With the custom forgot and reset password functionality in place, test it by running the application and verifying that the custom email template, notification, and reset password logic are working as expected.

 

Conclusion

In this tutorial, we have shown how to create a custom forgot and reset password system in Laravel. By following these steps, you can easily tailor the default Laravel authentication system to meet your specific needs.

Leave a Reply

Your email address will not be published. Required fields are marked *