The issue you're experiencing is because the user is not being logged in immediately after registration. To ensure that the user is logged in right after they register, you need to use the Auth::login method in your store method of the RegisterUser controller.
Here's how you can modify your store method to log in the user immediately after they are registered:
public function store(Request $request)
{
$attributes = $request->validate([
'first-name' => ['required'],
'last-name' => ['required'],
'email' => ['required', 'email'],
'password' => ['required', Password::min(6), 'confirmed'],
]);
$name = $attributes['first-name'] . ' ' . $attributes['last-name'];
$userData = [
'name' => $name,
'email' => $attributes['email'],
'password' => Hash::make($attributes['password']),
];
$user = User::create($userData);
// Log the user in
Auth::login($user);
return redirect('/jobs');
}
This will ensure that the user is authenticated immediately after registration, and Auth::user() will return the authenticated user.
Additionally, make sure that your session is properly configured in config/session.php and that you have the web middleware applied to your routes, as this middleware is responsible for handling sessions and authentication.
Here is a quick check for your routes to ensure they are using the web middleware:
Route::middleware(['web'])->group(function () {
Route::view('/', 'pages.home');
Route::view('/contact', 'pages.contact');
Route::resource('jobs', JobController::class);
Route::get('/register', [RegisterUser::class, 'create']);
Route::post('/register', [RegisterUser::class, 'store']);
Route::get('/login', [SessionController::class, 'create']);
Route::post('/login', [SessionController::class, 'store']);
});
By ensuring the user is logged in immediately after registration and verifying that your routes are using the web middleware, your @guest and @auth Blade directives should work as expected.