Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect user to login page when session expires in Laravel

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?

like image 597
three3 Avatar asked Nov 13 '17 21:11

three3


People also ask

How to custom login logout and redirect to login page in Laravel?

Laravel auth Built on Bootstrap 4. in this post, you will learn how to custom user logout and redirect to the login page. First open App\Http\Controllers\Auth\LoginController.php and insert below code: Now, open routes\web.php and add the below code: And Last, open resources\views\layouts\app.blade.php and put the below code:

What is the value of $redirectto in Laravel?

You can see that a $redirectTo variable exists and has the value of /home where users are redirected after they are logged in. In the Laravel built-in authentication system, you can customize many sides such as the redirection route using the $redirectTo variable which exists in both the login and registration controllers.

How to redirect the user if their session is expired or expired?

Follow the following steps and logout and redirect the user if their session is expired or session timeout: This command will create a middleware name SessionExpired.php. Next find app/Http / Middleware / SessionExpired.php & update the following code into your middleware file:

What happens to the previous page after login?

After login user you can redirect his previous page as he was before. It is very important for us because if you don’t know how to do it , then a user after login will be redirected to the home page which is not user friendly system in our application. So now let’s start tutorial.


1 Answers

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')]));
    }
}
like image 90
Camilo Avatar answered Sep 18 '22 00:09

Camilo