From 0152c6a2d3721c6ca594172320d83d498f3ca812 Mon Sep 17 00:00:00 2001 From: Wailan Tirajoh <53980548+WailanTirajoh@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:56:26 +0700 Subject: [PATCH] feat: forgot password --- app/Http/Controllers/AuthController.php | 43 +++++++++++++++++++ resources/views/auth/login.blade.php | 2 +- .../views/auth/passwords/email.blade.php | 19 +++++++- ...set.blade.php => reset-password.blade.php} | 19 +++++++- routes/web.php | 14 +++++- 5 files changed, 93 insertions(+), 4 deletions(-) rename resources/views/auth/{passwords/reset.blade.php => reset-password.blade.php} (90%) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index f657444..a83a584 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -3,8 +3,12 @@ namespace App\Http\Controllers; use App\Http\Requests\PostLoginRequest; +use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Auth\Events\PasswordReset; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Password; class AuthController extends Controller { @@ -22,4 +26,43 @@ public function logout() Auth::logout(); return redirect('login')->with('success', 'Logout success, goodbye ' . $name); } + + public function forgotPassword(Request $request) + { + $request->validate(['email' => 'required|email']); + + $status = Password::sendResetLink( + $request->only('email') + ); + + return $status === Password::RESET_LINK_SENT + ? back()->with(['status' => __($status)]) + : back()->withErrors(['email' => __($status)]); + } + + public function resetPassword(Request $request) + { + $request->validate([ + 'token' => 'required', + 'email' => 'required|email', + 'password' => 'required|min:8|confirmed', + ]); + + $status = Password::reset( + $request->only('email', 'password', 'password_confirmation', 'token'), + function (User $user, string $password) { + $user->forceFill([ + 'password' => Hash::make($password) + ])->setRememberToken(Str::random(60)); + + $user->save(); + + event(new PasswordReset($user)); + } + ); + + return $status === Password::PASSWORD_RESET + ? redirect()->route('login')->with('status', __($status)) + : back()->withErrors(['email' => [__($status)]]); + } } diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index daf2d5a..9261488 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -84,7 +84,7 @@ class="form-control @error('password') is-invalid @enderror" placeholder="Passwo
- {{--

Doesnt have any account? register

--}} +

Forgot Password? go here

diff --git a/resources/views/auth/passwords/email.blade.php b/resources/views/auth/passwords/email.blade.php index 1fea984..18eb46b 100644 --- a/resources/views/auth/passwords/email.blade.php +++ b/resources/views/auth/passwords/email.blade.php @@ -1,6 +1,23 @@ -@extends('layouts.app') +@extends('template.auth') @section('content') + +
diff --git a/resources/views/auth/passwords/reset.blade.php b/resources/views/auth/reset-password.blade.php similarity index 90% rename from resources/views/auth/passwords/reset.blade.php rename to resources/views/auth/reset-password.blade.php index 989931d..f6e5a9f 100644 --- a/resources/views/auth/passwords/reset.blade.php +++ b/resources/views/auth/reset-password.blade.php @@ -1,6 +1,23 @@ -@extends('layouts.app') +@extends('template.auth') @section('content') + +
diff --git a/routes/web.php b/routes/web.php index 08a674f..a49b374 100644 --- a/routes/web.php +++ b/routes/web.php @@ -82,12 +82,24 @@ Route::get('/mark-all-as-read', [NotificationsController::class, 'markAllAsRead'])->name('notification.markAllAsRead'); - Route::get('/notification-to/{id}',[NotificationsController::class, 'routeTo'])->name('notification.routeTo'); + Route::get('/notification-to/{id}', [NotificationsController::class, 'routeTo'])->name('notification.routeTo'); }); +// Login routes Route::view('/login', 'auth.login')->name('login'); Route::post('/postLogin', [AuthController::class, 'postLogin'])->name('postlogin'); +// Forgot Password routes +Route::group(['middleware' => 'guest'], function () { + Route::get('/forgot-password', fn () => view('auth.passwords.email'))->name('password.request'); + Route::post('/forgot-password', [AuthController::class, 'forgotPassword'])->name('password.email'); + + // Reset Password routes + Route::get('/reset-password/{token}', fn (string $token) => view('auth.reset-password', ['token' => $token])) + ->name('password.reset'); + Route::post('/reset-password', [AuthController::class, 'resetPassword'])->name('password.update'); +}); + Route::get('/', [HomeController::class, 'index'])->name('home'); Route::get('/sendEvent', function () {