Skip to content

Redirect back to original destination after login in Laravel

Last updated on December 21, 2022

In this post, I want to show you some basic tips for redirecting users back to the original destination path or some defined path with simple steps.

If you want to send the user to the previous original destination URL after login. Laravel stores and updates each entry page in the Session, so as soon as a login is successful, you can do

return Redirect::intended();

just in case the intended is not available at the moment, we can set the redirect URL.

return Redirect::intended('admin/dashboard');

And you can change the original intended by setting a new one using Session

Session::put('url.intended', 'newURL');

Once you have authenticated the user using Auth::check() you’ll be able to grab the authenticated user with Auth::user(). so based on Auth::user() data you can redirect users to a user-specific page.

Here is a simple way of redirecting.

if (Auth::guest())  {
    return Redirect::to('login');
} else {
    return Redirect::to('profile');
}

That’s it. Thank you! Happy coding…..

0 0 votes
Article Rating
Subscribe
Notify of
guest

4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments