From 1286e4851cc952faf332495269dfaff99c7f8596 Mon Sep 17 00:00:00 2001 From: hussainali Date: Mon, 6 May 2024 04:03:25 +0500 Subject: [PATCH 1/2] solved by Hussain Ali Hussain --- .../Controllers/Auth/RegisteredUserController.php | 2 +- app/Http/Controllers/ProfileController.php | 7 +++++++ app/Models/User.php | 2 +- resources/views/auth/profile.blade.php | 4 ++-- resources/views/layouts/navigation.blade.php | 2 ++ routes/web.php | 14 ++++++++++---- 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Auth/RegisteredUserController.php b/app/Http/Controllers/Auth/RegisteredUserController.php index d8d29eb5..11ce4191 100644 --- a/app/Http/Controllers/Auth/RegisteredUserController.php +++ b/app/Http/Controllers/Auth/RegisteredUserController.php @@ -37,7 +37,7 @@ public function store(Request $request) $request->validate([ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], - 'password' => ['required', 'confirmed', Rules\Password::defaults()], + 'password' => ['required', 'confirmed', 'regex:/^[a-zA-Z].*/'], ]); $user = User::create([ diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index e0093a49..6033cafa 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -15,6 +15,13 @@ public function update(ProfileUpdateRequest $request) { // Task: fill in the code here to update name and email // Also, update the password if it is set + $validated = $request->validated(); + + if($validated['password'] ?? false) { + $validated['password'] = bcrypt($validated['password']); + } + + auth()->user()->update($validated); return redirect()->route('profile.show')->with('success', 'Profile updated.'); } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..12a2354b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -8,7 +8,7 @@ use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; -class User extends Authenticatable +class User extends Authenticatable implements MustVerifyEmail { use HasApiTokens, HasFactory, Notifiable; diff --git a/resources/views/auth/profile.blade.php b/resources/views/auth/profile.blade.php index 70e8a8e1..cf98a63a 100644 --- a/resources/views/auth/profile.blade.php +++ b/resources/views/auth/profile.blade.php @@ -29,7 +29,7 @@ class="block mt-1 w-full" type="text" name="name" - value="???" + :value="auth()->user()->name" required /> @@ -40,7 +40,7 @@ class="block mt-1 w-full" class="block mt-1 w-full" type="email" name="email" - value="???" + :value="auth()->user()->email" required /> diff --git a/resources/views/layouts/navigation.blade.php b/resources/views/layouts/navigation.blade.php index 785936e7..6323af4c 100644 --- a/resources/views/layouts/navigation.blade.php +++ b/resources/views/layouts/navigation.blade.php @@ -16,9 +16,11 @@ {{ __('Users') }} {{-- Task: this "Profile" link should be visible only to logged-in users --}} + @auth {{ __('Profile') }} + @endauth diff --git a/routes/web.php b/routes/web.php index 71d04297..d95904c0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -20,17 +20,23 @@ Route::get('users', [\App\Http\Controllers\UserController::class, 'index'])->name('users.index'); // Task: profile functionality should be available only for logged-in users -Route::get('profile', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show'); -Route::put('profile', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update'); +Route::middleware('auth')->group(function () { + Route::get('profile', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show'); + Route::put('profile', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update'); +}); // Task: this "/secretpage" URL should be visible only for those who VERIFIED their email // Add some middleware here, and change some code in app/Models/User.php to enable this +Route::middleware(['auth', 'verified'])->group(function () { Route::view('/secretpage', 'secretpage') ->name('secretpage'); +}); // Task: this "/verysecretpage" URL should ask user for verifying their password once again // You need to add some middleware here -Route::view('/verysecretpage', 'verysecretpage') - ->name('verysecretpage'); +Route::middleware('password.confirm')->group(function () { + Route::view('/verysecretpage', 'verysecretpage') + ->name('verysecretpage'); +}); require __DIR__.'/auth.php'; From ded8d397febe7943b4200aac646a0b970907a6c9 Mon Sep 17 00:00:00 2001 From: hussainali Date: Mon, 6 May 2024 04:06:14 +0500 Subject: [PATCH 2/2] added min:8 to the rule --- app/Http/Controllers/Auth/RegisteredUserController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Auth/RegisteredUserController.php b/app/Http/Controllers/Auth/RegisteredUserController.php index 11ce4191..23961e38 100644 --- a/app/Http/Controllers/Auth/RegisteredUserController.php +++ b/app/Http/Controllers/Auth/RegisteredUserController.php @@ -37,7 +37,7 @@ public function store(Request $request) $request->validate([ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], - 'password' => ['required', 'confirmed', 'regex:/^[a-zA-Z].*/'], + 'password' => ['required', 'confirmed', 'min:8', 'regex:/^[a-zA-Z].*/'], ]); $user = User::create([