From bd39d143d6bab0e0d4c81fce1591bd21a0d92847 Mon Sep 17 00:00:00 2001 From: Ankit Rastogi Date: Tue, 10 Aug 2021 10:13:11 +0530 Subject: [PATCH] Fifth commit --- app/Http/Controllers/Auth/LoginController.php | 71 +++++++++++++++++++ app/Mail/ForgetPasswordMail.php | 40 +++++++++++ app/Models/ResetPassword.php | 16 +++++ ...09_140725_create_reset_passwords_table.php | 33 +++++++++ resources/css/app.css | 5 +- .../views/auth/forget_password.blade.php | 41 +++++++++++ resources/views/auth/login.blade.php | 3 +- .../emails/forget_pasword_mail.blade.php | 15 ++++ .../views/emails/reset_password.blade.php | 47 ++++++++++++ routes/web.php | 12 ++-- 10 files changed, 277 insertions(+), 6 deletions(-) create mode 100644 app/Mail/ForgetPasswordMail.php create mode 100644 app/Models/ResetPassword.php create mode 100644 database/migrations/2021_08_09_140725_create_reset_passwords_table.php create mode 100644 resources/views/auth/forget_password.blade.php create mode 100644 resources/views/emails/forget_pasword_mail.blade.php create mode 100644 resources/views/emails/reset_password.blade.php diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 5aa7eb4..912a534 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -2,11 +2,17 @@ namespace App\Http\Controllers\Auth; +use App\Models\User; use App\Models\VerifyUser; +use Illuminate\Support\Str; use Illuminate\Http\Request; +use App\Models\ResetPassword; use Illuminate\Support\Carbon; use App\Http\Controllers\Controller; +use App\Mail\ForgetPasswordMail; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Mail; class LoginController extends Controller { @@ -53,4 +59,69 @@ public function verifyEmail($token) return redirect(route('user.login'))->with('error', 'Something went wrong!!'); } } + + public function getForgetPassword() + { + return view('auth.forget_password'); + } + public function postForgetPassword(Request $request) + { + $this->validate($request, [ + 'email' => 'required|email', + ]); + + $user = User::where('email', $request->email)->first(); + + if (!$user) { + return redirect()->back()->with('error', 'User not found.'); + } else { + $reset_code = Str::random(200); + ResetPassword::create([ + 'user_id' => $user->id, + 'reset_code' => $reset_code + ]); + + Mail::to($user->email)->send(new ForgetPasswordMail($user->name, $reset_code)); + + return redirect()->back()->with('success', 'We have sent you a password reset link.Please check your mail.'); + } + } + + public function getResetPassword($reset_code) + { + $password_reset_code = ResetPassword::where('reset_code', $reset_code)->first(); + if (!$password_reset_code || Carbon::now()->subMinutes(50) > $password_reset_code->created_at) { + return redirect()->route('getForgetPassword')->with('error', 'Invalid password reset link or link expired'); + } else { + return view('emails.reset_password', compact('reset_code')); + } + } + + public function postResetPassword($reset_code, Request $request) + { + $password_reset_code = ResetPassword::where('reset_code', $reset_code)->first(); + + if (!$password_reset_code || Carbon::now()->subMinutes(50) > $password_reset_code->created_at) { + return redirect()->route('getForgetPassword')->with('error', 'Invalid password reset link or link expired'); + } else { + $request->validate([ + 'email' => 'required|email', + 'password' => 'required', + 'password_confirmation' => 'required|same:password', + ]); + + $user = User::find($password_reset_code->user_id); + + if ($user->email != $request->email) { + return redirect()->back()->with('error', 'Enter Correct Email'); + } else { + $password_reset_code->delete(); + $user->update([ + 'password' => Hash::make($request->password) + ]); + + return redirect()->route('user.login')->with('success', 'Password succesfully reset'); + } + } + } } diff --git a/app/Mail/ForgetPasswordMail.php b/app/Mail/ForgetPasswordMail.php new file mode 100644 index 0000000..4028a6c --- /dev/null +++ b/app/Mail/ForgetPasswordMail.php @@ -0,0 +1,40 @@ +user_name = $user_name; + $this->reset_code = $reset_code; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this->markdown('emails.forget_pasword_mail')->with([ + 'user_name' => $this->user_name, + 'reset_code' => $this->reset_code + ]); + } +} diff --git a/app/Models/ResetPassword.php b/app/Models/ResetPassword.php new file mode 100644 index 0000000..f940a4b --- /dev/null +++ b/app/Models/ResetPassword.php @@ -0,0 +1,16 @@ +id(); + $table->unsignedBigInteger('user_id'); + $table->string('reset_code'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('reset_passwords'); + } +} diff --git a/resources/css/app.css b/resources/css/app.css index bd6213e..135056d 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -1,3 +1,6 @@ @tailwind base; + @tailwind components; -@tailwind utilities; \ No newline at end of file + +@tailwind utilities; + diff --git a/resources/views/auth/forget_password.blade.php b/resources/views/auth/forget_password.blade.php new file mode 100644 index 0000000..dbfdd30 --- /dev/null +++ b/resources/views/auth/forget_password.blade.php @@ -0,0 +1,41 @@ +@extends('layouts.app') + +@section('content') +
+
+ @if (session('error')) +
+ {{ session('error') }} +
+ @endif + @if (session('success')) +
+ {{ session('success') }} +
+ @endif + @if (session('message')) +
+ {{ session('message') }} +
+ @endif +
+ @csrf + +
+ + + + @error('email') +
+ {{ $message }} +
+ @enderror +
+ +
+ +
+
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index 8bc99a2..bde5c10 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -50,9 +50,10 @@ -
+
+
Forgot Password ?
diff --git a/resources/views/emails/forget_pasword_mail.blade.php b/resources/views/emails/forget_pasword_mail.blade.php new file mode 100644 index 0000000..bb2c709 --- /dev/null +++ b/resources/views/emails/forget_pasword_mail.blade.php @@ -0,0 +1,15 @@ +@component('mail::message') + +Hello {{$user_name}} + +Click here to reset your password + +@component('mail::button', ['url' => route('getResetPassword',$reset_code)]) +Reset Password +@endcomponent +

Or copy & paste the following link to your browser

+

{{route('getResetPassword',$reset_code)}}

+ +Thanks,
+{{ config('app.name') }} +@endcomponent diff --git a/resources/views/emails/reset_password.blade.php b/resources/views/emails/reset_password.blade.php new file mode 100644 index 0000000..a647ee0 --- /dev/null +++ b/resources/views/emails/reset_password.blade.php @@ -0,0 +1,47 @@ +@extends('layouts.app') + +@section('content') +
+
+
+ @csrf + +
+ + + + @error('email') +
+ {{ $message }} +
+ @enderror +
+ +
+ + + + @error('password') +
+ {{ $message }} +
+ @enderror +
+ +
+ + + + @error('password_confirmation') +
+ {{ $message }} +
+ @enderror +
+
+ +
+
+
+
+@endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 1c39841..7f4a155 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,22 +1,21 @@ name('home'); -// Auth::routes(['verify' => true]); - Route::get('/dashboard', [DashboardController::class, 'index']) ->name('dashboard'); @@ -27,6 +26,11 @@ Route::get('/login', [LoginController::class, 'index'])->name('user.login'); Route::post('/login/validate', [LoginController::class, 'validateLogin'])->name('user.validate'); Route::get('/user/verify/{token}', [LoginController::class, 'verifyEmail'])->name('user.verify'); +Route::get('/forget-password', [LoginController::class, 'getForgetPassword'])->name('getForgetPassword'); +Route::post('/forget-password', [LoginController::class, 'postForgetPassword'])->name('postForgetPassword'); +Route::get('/reset-password/{reset_code}', [LoginController::class, 'getResetPassword'])->name('getResetPassword'); +Route::post('/reset-password/{reset_code}', [LoginController::class, 'postResetPassword'])->name('postResetPassword'); + Route::get('/register', [RegisterController::class, 'index'])->name('register'); Route::post('/register', [RegisterController::class, 'store']);