To address the issue of displaying a session message on the login page when a session has expired, you need to ensure that the session variable is correctly set and accessed. Let's go through the steps to fix this:
-
Middleware: Your middleware is correctly catching the
TokenMismatchExceptionand setting a session variable. However, ensure that the session key used is consistent throughout your code.class HandleTokenMismatch { public function handle($request, Closure $next) { try { return $next($request); } catch (TokenMismatchException $e) { session(['session_error' => 'Your session has expired. Please log in again']); return redirect()->route('login'); } } } -
Login Controller: In your
showLoginFormmethod, you are retrieving the session message correctly, but you need to ensure that the session key used in the Blade template matches the one set in the middleware.public function showLoginForm(Request $request) { if (Auth::check()) { return redirect()->route('admin'); } $sessionError = $request->session()->get('session_error'); return view('auth.login', compact('sessionError')); } -
Blade Template: In your Blade template, ensure that you are checking for the correct session variable. You should use the variable name passed from the controller.
@if ($sessionError) <div class="alert alert-info"> {{ $sessionError }} </div> @endif
By ensuring that the session key is consistent across your middleware, controller, and Blade template, you should be able to display the session expiration message correctly on the login page.