Hope, you have already installed the Laravel project and any default authentication packages in Laravel like ui, breez, etc

First Create a Controller 

php artisan make:controller User/ForgetPasswordManagerController

Once successfully created a controller then go to the web file and use it. and make a route for the form password reset

use App\Http\Controllers\User\ForgetPasswordManagerController;

//Forget password

Route::get('/forget-password',[ForgetPasswordManagerController::class,'forgetPassword'])->name('forget.password');

Now go to the controller file and create a function forgetPassword

 public function forgetPassword(){

        return view('frontend.forget-password');

    }//End Method

Create a folder name frontend  and under the frontend folder create a file name forget-password'.blade.php

Now let's design the forget-password. balde.php file 

@extends('layouts.front.front')

@section('title')

Forget Password

@endsection

@section('content')

<section class="main-content">

<div class="container-xl">

  <!-- contact-area -->

  <div class="contact-area">

                <div class="container">

                    <p>We will send a link to your email, use this link to reset your password!</p>

                    <div class="mt-5">

                        @if($errors->any())

                        <div class="col-12">

                            @foreach(@errors->all() as $error)

                                    <div class="alert alert-danger">{{$error}}</div>

                            @endforeach

                        </div>

                        @endif

                        @if(session()->has('error'))

                            <div class="alert alert-danger">{{session('error')}}</div>

                        @endif

                        @if(session()->has('success'))

                            <div class="alert alert-success">{{session('success')}}</div>

                        @endif

                    </div>

                    <form action="#" method= "POST" class="contact__form">

                        @csrf

                    <div class="row">

                            <div class="col-md-6">

                                <input type="email" name="email" class="form-control" placeholder="Enter your Email*">

                            </div>

                        </div>

                        <button type="submit" class="btn btn-success btn-lg mt-2">send massage</button>

                    </form>

                </div>

</div>

</div>

</section>

@endsection

Now link the url 

Note: Here I am using Laravel ui Auth package and I am using the default login and register page.

views\auth\login.blade.php

@extends('layouts.app')

@section('content')

<div class="container">

    <div class="row justify-content-center">

        <div class="col-md-8">

            <div class="card">

                <div class="card-header">{{ __('Login') }}</div>

                <div class="card-body">

                    <form method="POST" action="{{ route('login') }}">

                        @csrf

                        <div class="row mb-3">

                            <label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label>

                            <div class="col-md-6">

                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>

                                @error('email')

                                    <span class="invalid-feedback" role="alert">

                                        <strong>{{ $message }}</strong>

                                    </span>

                                @enderror

                            </div>

                        </div>

                        <div class="row mb-3">

                            <label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label>

                            <div class="col-md-6">

                                <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">

                                @error('password')

                                    <span class="invalid-feedback" role="alert">

                                        <strong>{{ $message }}</strong>

                                    </span>

                                @enderror

                            </div>

                        </div>

                        <div class="row mb-3">

                            <div class="col-md-6 offset-md-4">

                                <div class="form-check">

                                    <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>

                                    <label class="form-check-label" for="remember">

                                        {{ __('Remember Me') }}

                                    </label>

                                </div>

                            </div>

                        </div>

                        <div class="row mb-0">

                            <div class="col-md-8 offset-md-4">

                                <button type="submit" class="btn btn-primary">

                                    {{ __('Login') }}

                                </button>

                             <a href="{{url('forget-password')}}">Forgret Password ?</a>

                            </div>

                        </div>

                    </form>

                </div>

            </div>

        </div>

    </div>

</div>

@endsection

Here I am using for login page default layout @extends('layouts.app');

Once the User clicks on forget password link the form will display.

Now create a POST route. so we have to go web.php file and paste the route as below

Route::post('/forget-password',[ForgetPasswordManagerController::class,'forgetPasswordPost'])->name('forget.password.post');

Create a Method forgetPasswordPost.

public function forgetPasswordPost(Request $request){

        $request->validate([

            'email'=>"required|email|exists:users",

        ]);

        $token = Str::random(64);

        DB::table('password_resets')->insert([

            'email'=>$request->email,

            'token'=>$token,

            'created_at'=>Carbon::now(),

        ]);

        Mail::send("emails.forget-password",['token'=>$token],function($message) use ($request){

            $message->to($request->email);

            $message->subject("Rest Password");

        });

        return redirect()->to(route('forget.password'))->with("success"," We have send an email to reset");

    }

Make sure we have to use Mail, Hash and DB Facades like below

use Illuminate\Support\Facades\Hash;

use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Mail;

use Illuminate\Support\Str;

Use App\Models\User;

use Carbon\Carbon;

Make sure all you have to use

 DB::table('password_resets') Check the table if you haven't the create a table with the field email,token,created_at

Now create a folder for emails and under the emails folder create a file forget-password.blade.php and paste the below code

resources→views→forget-password.blade.php

<html>
<h1>Reset Password</h1>    
<p>Click the link below to reset the password fro your account.</p>
   <a href="{{route('reset.password',$token)}}">Reset Password</a>
</html>

This Html template sends to the user's email with a token.

Now create another route like below with the get method.

Route::get('/reset-passwd/{token}',[ForgetPasswordManagerController::class,'resetPassword'])->name('reset.password');

When the user clicks on the email link it will redirect to the reset password page.

Now go to the controller page and paste the below function

public function resetPassword($token){

        return view('frontend.new-password',compact('token'));

    }//End Method

Now create file new-password.blade.php under the frontend folder.

Note: frontend folder I have created as per my requirement you can create any folder name or directly you can use views folder.

new-password.blade.php

@extends('layouts.front.front')

@section('title')

New Password

@endsection

@section('content')

<section class="main-content">

<div class="container-xl">

  <!-- contact-area -->

  <div class="contact-area">

                <div class="container">

                    <p>We will send a link to your email, use this link to reset your password!</p>

                    <div class="mt-5">

                    @if(count($errors))

                @foreach ($errors->all() as $error)

                <p class="alert alert-danger alert-dismissible fade show"> {{ $error}} </p>

                @endforeach

            @endif

                        @if(session()->has('error'))

                            <div class="alert alert-danger">{{session('error')}}</div>

                        @endif

                        @if(session()->has('success'))

                            <div class="alert alert-success">{{session('success')}}</div>

                        @endif

                    </div>

                    <form action="{{route('reset.password.post')}}" method= "POST" class="contact__form">

                        @csrf

                        <input type="hidden" name="token" value="{{$token}}">

                    <div class="row">

                            <div class="col-md-6">

                                <input type="email" name="email" class="form-control" placeholder="Enter your Email*">

                            </div>

                            <div class="col-md-6">

                                <input type="password" name="password" class="form-control" placeholder="New Password*">

                            </div>

                            <div class="col-md-6">

                                <input type="password" name="password_confirmation" class="form-control" placeholder="Confirm Password*">

                            </div>

                        </div>

                        <button type="submit" class="btn btn-success btn-lg mt-2">send massage</button>

                    </form>

                </div>

</div>

</div>

</section>

@endsection

 <form action="{{route('reset.password.post')}}" method= "POST" class="contact__form">

Now create another POST method route for updating the password

Route::post('/reset-passwd',[ForgetPasswordManagerController::class,'resetPasswordPost'])->name('reset.password.post');

And his method like below

public function resetPasswordPost(Request $request){
           $request->validate([
               "email"=>"required|email|exists:users",
               "password"=>"required|string|min:6|confirmed",
               "password_confirmation"=>"required",
           ]);
           $updatePassword= DB::table('password_resets')->where([
               "email"=>$request->email,
               "token"=>$request->token,
           ])->first();

           if(!$updatePassword){
               return redirect()->to(route('forget.password'))->with("error"," Invalid Email or Token!");
           }
           User::where("email",$request->email)
           ->update(["password"=>Hash::make($request->password)]);
           DB::table('password_resets')->where(["email"=>$request->email])->delete();

           $notification = array(
               'message' => 'Password Successfully Changed!!', 
               'alert-type' => 'info'
           );
           return redirect()->to(route('login'))->with($notification);
   }//End Method

Go to the .env file and set SMTP like below

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourmail@gmail.com
MAIL_PASSWORD=dddxsdfdfdfdf // Your gmail app password created by gmail
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=yourmail@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

 

Leave a Comment
No Comments found