From d0716a95164ecccfa10d06d26459da3c9f1c4593 Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Wed, 28 Apr 2021 14:13:06 -0400 Subject: [PATCH] Add password reset routes --- app/Http/Controllers/UserController.php | 43 +++++++++++++++++++ .../2014_10_12_000000_create_users_table.php | 2 + resources/js/main.js | 2 +- resources/views/request-reset.blade.php | 17 ++++++++ resources/views/reset-password.blade.php | 17 ++++++++ routes/web.php | 20 ++++++--- 6 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 resources/views/request-reset.blade.php create mode 100644 resources/views/reset-password.blade.php diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 621010a..cb47a6e 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -7,6 +7,7 @@ use App\Models\User; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Hash; use Illuminate\Auth\Events\Registered; +use Illuminate\Support\Facades\Password; class UserController extends Controller { @@ -21,6 +22,7 @@ class UserController extends Controller $user = new User; $user->name = $request->name; $user->email = $request->email; + $user->role = "client"; $user->password = Hash::make($request->password); $user->save(); @@ -29,4 +31,45 @@ class UserController extends Controller "status" => "success" ]); } + + public function forgotPassword(Request $request) { + $request->validate(['email' => 'required|email']); + + $status = Password::sendResetLink( + $request->only('email') + ); + + if ($status === Password::RESET_LINK_SENT) { + return response()->json([ + "status" => "success" + ]) + } else { + return response()->json($errors->all()) + } + } + + 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, $password) use ($request) { + $user->forceFill([ + 'password' => Hash::make($password) + ])->setRememberToken(Str::random(60)) + }); + if ($status == Password::PASSWORD_RESET) { + return response()->json([ + "status" => "success" + ]); + } else { + return response()->json([__($status)]); + } + } } diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 621a24e..c7a50ff 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -19,6 +19,8 @@ class CreateUsersTable extends Migration $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); + $table->string('role'); + $table->bigInteger('credits'); $table->rememberToken(); $table->timestamps(); }); diff --git a/resources/js/main.js b/resources/js/main.js index 09ff43f..49f1945 100644 --- a/resources/js/main.js +++ b/resources/js/main.js @@ -18,7 +18,7 @@ function register(event) { 'X-XSRF-TOKEN': token}, body: JSON.stringify({"name": document.getElementById("register-name").value, "email": document.getElementById("register-email").value, - "password": document.getElementById("register-password").value}) + "password": document.getElementById("register-password").value}), "password_confirmation": document.getElementById("confirm-password").value}) }).then(response => { console.log('sent registration'); diff --git a/resources/views/request-reset.blade.php b/resources/views/request-reset.blade.php new file mode 100644 index 0000000..9484363 --- /dev/null +++ b/resources/views/request-reset.blade.php @@ -0,0 +1,17 @@ +@EXTEnds('master') + +@section('title', 'Password Reset') + +@section('head-metas') + @parent + +@endsection + +@section('content') +Reset form goes here. +@endsection + +@section('scripts') + @parent + +@endsection diff --git a/resources/views/reset-password.blade.php b/resources/views/reset-password.blade.php new file mode 100644 index 0000000..da9415d --- /dev/null +++ b/resources/views/reset-password.blade.php @@ -0,0 +1,17 @@ +@extends('master') + +@section('title', 'Password Reset') + +@section('head-metas') + @parent + +@endsection + +@section('content') +Reset form goes here. +@endsection + +@section('scripts') + @parent + +@endsection diff --git a/routes/web.php b/routes/web.php index 2434a31..488268e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -35,10 +35,20 @@ Route::post('/resend-verification', function (Request $request) { $request->user()->sendEmailVerificationNotification(); return back()->with('message', 'Verification link sent!'); -})->middleware(['auth', 'throttle:6,1'])->name('verification.send') - -Route::get('/login', function () { - return view('home'); -}); +})->middleware(['auth', 'throttle:6,1'])->name('verification.send'); Route::post('/register', [UserController::class, 'create']); + +Route::get('/forgot-password', function () { + return view('request-reset'); +})->middleware('guest')->name('password.request'); + +Route::post('/forgot-passowrd', [UserController::class, + 'forgotPassword'])->middleware('guest')->name('password.email'); + +Route::get('/reset-password/{token}', function ($token) { + return view('reset-password', ['token' => $token]); +})->middleware('guest')->name('password.reset'); + +Route::post('/reset-passowrd', [UserController::class, + 'resetPassword'])->middleware('guest')->name('password.email');