In this tutorial you will learn about the Laravel 8 Angular JWT Password Reset with Mailtrap Example and its application with practical example.
In this Laravel 8 Angular JWT Password Reset with Mailtrap Example Tutorial, I will show you how to create reset password feature in the Laravel 8 angular application with JWT auth and mailtrap. In this tutorial you will learn to create a password reset functionality in laravel and Angular using REST API with JWT Authentication and Mailtrap.
In this example we will create laravel APIs for reset password request and to reset the password. We will capture password reset request with the token. We will be using mailtrap to send password reset mail.
Laravel 8 Angular JWT Password Reset with Mailtrap Example
In this step by step tutorial I will demonstrate you how to create a password reset functionality with mailtrap in Angular using Laravel 8 REST API with JWT Authentication
- Clone Laravel Token-Based Authentication Repo
- Configure Mailtrap
- Reset Password Request
- Reset Password
Clone Laravel Token-Based Authentication Repo
In this step we will clone the repo using the following command:
1 |
git clone https://github.com/SinghDigamber/laravel-angular-jwt-auth.git |
Configure Mailtrap
Then, open .env
and add the mailtrap details.
1 2 3 4 5 6 7 |
MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME= // mailtrap username MAIL_PASSWORD= // mailtrap password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS= // email from address |
Password Reset Request
In this step we will create a controller PasswordResetRequestController using following command:
1 |
php artisan make:controller PasswordResetRequestController |
Open PasswordResetRequestController.php and place the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; use App\Models\User; use App\Mail\SendMail; use Illuminate\Support\Facades\Mail; use Carbon\Carbon; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; class PasswordResetRequestController extends Controller { public function sendPasswordResetEmail(Request $request){ // If email does not exist if(!$this->validEmail($request->email)) { return response()->json([ 'message' => 'Email does not exist.' ], Response::HTTP_NOT_FOUND); } else { // If email exists $this->sendMail($request->email); return response()->json([ 'message' => 'Check your inbox, we have sent a link to reset email.' ], Response::HTTP_OK); } } public function sendMail($email){ $token = $this->generateToken($email); Mail::to($email)->send(new SendMail($token)); } public function validEmail($email) { return !!User::where('email', $email)->first(); } public function generateToken($email){ $isOtherToken = DB::table('recover_password')->where('email', $email)->first(); if($isOtherToken) { return $isOtherToken->token; } $token = Str::random(80);; $this->storeToken($token, $email); return $token; } public function storeToken($token, $email){ DB::table('recover_password')->insert([ 'email' => $email, 'token' => $token, 'created' => Carbon::now() ]); } } |
Next we will create a mailable class for mail template and sending the mail.
1 |
php artisan make:mail SendMail --markdown=Email.resetPassword |
Open App/Mail/SendMail.php and put the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class SendMail extends Mailable { use Queueable, SerializesModels; public $token; /** * Create a new message instance. * * @return void */ public function __construct($token) { $this->token = $token; } /** * Build the message. * * @return $this */ public function build(){ return $this->markdown('Email.resetPassword')->with([ 'token' => $this->token ]); } } |
Consume Password Reset Request API
Next go to frontend/app/shared/auth.service.ts and put the following code in it: