6

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');
}

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.';
    }
}

Nothing I have tried works. I keep getting the exact same error message even though I am passing in the required parameters.

Update #1

Screenshot of error page:

enter image description here

Update #2

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

enter image description here

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

halfer
  • 19,824
  • 17
  • 99
  • 186
three3
  • 2,756
  • 14
  • 57
  • 85
  • Did you try to clear route cache? `php artisan route:clear` – Andriy Sushchyk Nov 13 '17 at 22:10
  • @AndriySushchyk Yes, I tried that but still received the same error message. Any other thoughts/suggestions? – three3 Nov 13 '17 at 22:13
  • How is `LoginController@show` defined? – Camilo Nov 13 '17 at 22:16
  • @Camilo Just updated the original post to show that code. – three3 Nov 13 '17 at 22:19
  • Are you sure the error is being thrown at the line you think it is? Everything looks fine for me. – Camilo Nov 13 '17 at 22:24
  • @Camilo Just updated the original post to show the screenshot of the error. – three3 Nov 13 '17 at 22:27
  • The screenshot doesn't really show where the error started. Could it be you are calling this route from somewhere else? – Camilo Nov 13 '17 at 22:30
  • @Camilo I am not sure to be honest. I only receive this error when a user is not logged in. When I am logged in, all of the routes work without any errors. – three3 Nov 13 '17 at 22:32
  • Comment that line out and try to reproduce the problem. If the same problem persists, I would look for the cause somewhere else. If not, then there is something we are not seeing. – Camilo Nov 13 '17 at 22:33
  • @Camilo Sorry, had to step out for a bit. I commented out that line like you said to and still the same error message. Someone else suggested that I try this: `return redirect()->route('login', ['demo']);` however that did not work either. I am soooo stumped it is driving me crazy lol. – three3 Nov 14 '17 at 01:23
  • 1
    @Camilo, please check updated post to see if the new information helps. Thanks – three3 Nov 14 '17 at 01:27

1 Answers1

8

You can override the unauthenticated() method in your app/Exceptions/Handler.php file to add the missing route parameter.

use Illuminate\Auth\AuthenticationException;

class Handler extends ExceptionHandler
{
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        return $request->expectsJson()
            ? response()->json(['message' => $exception->getMessage()], 401)
            : redirect()->guest(route('login', ['account' => $request->route('account')]));
    }
}
Camilo
  • 6,504
  • 4
  • 39
  • 60
  • Thank you. I did this and now I am receiving a new error message (this is progress :) The new error message is: `ErrorException (E_WARNING) Declaration of App\Exceptions\Handler::unauthenticated($request, App\Exceptions\AuthenticationException $exception) should be compatible with Illuminate\Foundation\Exceptions\Handler::unauthenticated($request, Illuminate\Auth\AuthenticationException $exception)`. – three3 Nov 14 '17 at 01:57
  • 1
    Add `use Illuminate\Auth\AuthenticationException;` to your `Handler.php` file. – Camilo Nov 14 '17 at 02:11
  • Awesome, no more error messages! 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 https://acme.ems.dev how do I get the `acme` part so that it will redirect to **their** login screen? – three3 Nov 14 '17 at 02:21
  • I just tried this and it redirects me to `https://ems.dev:8888/`. It simply removes the subdomain. Any other suggestions? We are close to figuring it out :) – three3 Nov 14 '17 at 02:36
  • I also tried `request()->input('account')` but no luck either, same result. – three3 Nov 14 '17 at 02:44
  • 1
    Try `$request->route('account')`. – Camilo Nov 14 '17 at 03:54
  • this solved my issue! Thank you very, very much for all of your help! – three3 Nov 14 '17 at 04:09
  • i tried similar method i put if $exception is an instance of AuthenticationException then redirect to login with some message like session has been expired,but now what happening is every time i visit login page it tell me session has expired. – Linus Dec 15 '17 at 10:50
  • @Linus Try this approach instead. – Camilo Dec 15 '17 at 14:25