Skip to content

Commit

Permalink
Completed the Laravel Auth basics tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
KunYuChang committed Dec 1, 2024
1 parent f0cc8fb commit 398838f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
11 changes: 6 additions & 5 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Validation\Rules;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules;
use Illuminate\Auth\Events\Registered;
use App\Providers\RouteServiceProvider;
use Illuminate\Validation\Rules\Password;

class RegisteredUserController extends Controller
{
Expand Down Expand Up @@ -37,7 +38,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', Password::defaults()->letters()],
]);

$user = User::create([
Expand Down
19 changes: 17 additions & 2 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Http\Requests\ProfileUpdateRequest;

class ProfileController extends Controller
Expand All @@ -11,10 +13,23 @@ public function show()
return view('auth.profile');
}

// KunYuChang
public function update(ProfileUpdateRequest $request)
{
// Task: fill in the code here to update name and email
// Also, update the password if it is set
// Get the authenticated user
$user = auth()->user();

// Update name and email
$user->name = $request->name;
$user->email = $request->email;

// Check if a new password is provided
if ($request->filled('password')) {
$user->password = bcrypt($request->password);
}

// Save the updated user information to the database
$user->save();

return redirect()->route('profile.show')->with('success', 'Profile updated.');
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions resources/views/auth/profile.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class="block mt-1 w-full"
type="text"
name="name"
value="???"
value="{{ Auth::user()->name }}"
required />
</div>

Expand All @@ -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 />
</div>

Expand Down
10 changes: 7 additions & 3 deletions resources/views/layouts/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
<x-nav-link :href="route('users.index')" :active="request()->routeIs('users.index')">
{{ __('Users') }}
</x-nav-link>
{{-- KunYuChang --}}
{{-- Task: this "Profile" link should be visible only to logged-in users --}}
<x-nav-link href="/profile" :active="request()->routeIs('profile.show')">
{{ __('Profile') }}
</x-nav-link>
@auth
<x-nav-link href="/profile" :active="request()->routeIs('profile.show')">
{{ __('Profile') }}
</x-nav-link>
@endauth

</div>
</div>

Expand Down
12 changes: 8 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@
return view('home');
})->name('home');

Route::get('users', [\App\Http\Controllers\UserController::class, 'index'])->name('users.index');
Route::get('users', [\App\Http\Controllers\UserController::class, 'index'])
->name('users.index');

// KunYuChang
// 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::get('profile', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show')->middleware('auth');
Route::put('profile', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update')->middleware('auth');

// 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::view('/secretpage', 'secretpage')
->name('secretpage');
->name('secretpage')
->middleware('verified');

// Task: this "/verysecretpage" URL should ask user for verifying their password once again
// You need to add some middleware here
Route::view('/verysecretpage', 'verysecretpage')
->middleware(['auth', 'password.confirm'])
->name('verysecretpage');

require __DIR__.'/auth.php';

0 comments on commit 398838f

Please sign in to comment.