How to get rid of /home URL for authentication?

This time – a really short tip. In default Laravel Auth functionality there are some predefined values, settings and similar stuff – one of them is redirecting to /home URL if a user is already logged in. What if we don’t have that /home – what if in our case it’s different?

Basically, if you don’t have /home URL in your routes.php file, you will get something like this:

0902_laravel_route_notfound

If you want to change that already-logged-in URL to a different one – let’s say, /account or just root URL /, you need this file:
app/Http/Middleware/RedirectIfAuthenticated.php:

    public function handle($request, Closure $next)
    {
        if ($this->auth->check()) {
            return redirect('/home');
        }

        return $next($request);
    }

Just change ‘/home’ here to whatever you want and that’s it. You can also use something like redirect()->route() or even some other functionality, not only redirect.

Update: thanks for the tip in the comments by Florian from Laramap (awesome project, by the way) and Francis – if you want to change the after-login URL, you should override a property in app/Http/Controllers/AuthController.php:

protected $redirectPath = '/dashboard';
Like our articles?
Check out our Laravel online courses!

5 COMMENTS

  1. What to do if i want to change content based on authentication satus. For Example, before authentication home page will have slider and other kind of static stuff but i want to fetch data from db if the user is logged in.

    Thanks in Advance.

LEAVE A REPLY

Please enter your comment!
Please enter your name here