Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dlubbat's avatar

Redirect User to Login Page When Session Expires

I am trying to redirect a user back to the login page if their session has expired. I am using Laravel 5.5. I have edited my RedirectIfAuthenticated file to include the following code in the handle function:

if (!Auth::check()) {
    return redirect()->route('login', ['account' => 'demo']);
}

When I do this, I am receiving the following error message:

Missing required parameters for [Route: login] [URI: /].

My login route is inside a subdomain route group which is why I am passing the account parameter. Here is part of my code in web.php:

// Subdomain routing
Route::domain('{account}.ems.dev')->group(function () {
    Route::get('/', 'LoginController@show')->name('login');
}

Any help is greatly appreciated! Nothing I have tried works. I keep getting the exact same error message even though I am passing in the required parameters.

And here is my LoginController@show code:

/*
 * Show the login form
 */
public function show($account) {
    // Validate this is a valid subdomain
    $organization = Organization::where('subdomain', $account)->first();

    if ($organization) { 
        return view('login');
    } else {
        return 'This account does not exist.';
    }
}

Screenshot of error page: https://i.stack.imgur.com/I5Z2U.png

UPDATE:

After a little digging around the Whoops! error page, I see this, protected function unauthenticated is what is causing the problem:

https://i.stack.imgur.com/CfrsR.png

How do I override this function to add the missing parameter?

0 likes
8 replies
TortleWortle's avatar

What happens when you just do return redirect()->route('login', ['demo']);?

robrogers3's avatar

so, as you have probably figured out, your wildcard or what not you are passing in is causing the error because the unauthenticated method doesn't pass any.

overriding the method is super simple: just add the unauthenticated method to your app/Exceptions/Handler (i think that's where it is). then change the redirect to match yours.

dlubbat's avatar

@robrogers3 Yes, I actually did this. Here is the code I used:

protected function unauthenticated($request, AuthenticationException $exception)
    {
        return $request->expectsJson()
            ? response()->json(['message' => $exception->getMessage()], 401)
            : redirect()->guest(route('login', ['account' => request()->input('account')]));
    }

This solved my original problem. However, there is one slight issue. The value of account is different for each user in my system. How do I get the account from the URL for proper redirection? For example, if the user's login URL is acme.ems.dev how do I get the acme part so that it will redirect to their login screen?

What I currently have, request()->input('account') redirects me to https://ems.dev:8888/. It simply removes the subdomain. Any other suggestions?

1 like
dlubbat's avatar
dlubbat
OP
Best Answer
Level 2

The solutions is this:

$request->route('account')
babonday's avatar

@DLUBBAT -

iam confused about this question... i would have thought , not telling a user his session timed out so they carry on working until they realise they have no session, would be high on the experience list? what is everyone else doing to inform users their session timed out?

Please or to participate in this conversation.