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

jrean's avatar

Redirect Intended

Hi,

How do you manage the intended redirect with the existing authentication system Laravel provides out of the box?

The App\Http\Controllers\Auth\AuthController.php uses the AuthenticatesAndRegistersUsers trait.

The public function postLogin(Request $request) says:

...
        if ($this->auth->attempt($credentials, $request->has('remember')))
        {
            return redirect()->intended($this->redirectPath());
        }
...     

I have a show method with:

        if (  !$foo->is_free && !app('auth')->check()) {
            return redirect('login');
        }

When I perform the return redirect('login'); what do I need to provide to have the return redirect()->intended($this->redirectPath()); intended working?

Thank you.

0 likes
8 replies
bobbybouwmann's avatar

So let's say you visit the url http://example.com/admin/post/create but you need to be logged in to visit this page (The route has a middleware of auth). The middleware kicks in and redirects you to the login page. Now when you login you will be redirected to the intented page and that is /admin/post/create.

jrean's avatar

@bobbybouwmann thank you for your answer but this is not what I meant.

I have a route /foo/{id} and a show method. Depending of the resource found a visitor will need to be authenticated to see the requested resource.

if (  !$foo->is_free && !app('auth')->check()) {
            return redirect('login');
}

So, if he is not authenticated he will be redirected to the login route (which is an alias of the original auth/login). Once the user is logged in I would like hime to be redirected to the requested resource with the intended functionality. For now with my code he is instead redirected to /home.

jrean's avatar
jrean
OP
Best Answer
Level 8

I found.

I needed to perform the following

return redirect()->guest('auth/login');
//or with my custom route:
return redirect()->guest('login');

Now the intended is working properly.

2 likes
bashy's avatar

Yeah you kinda have it backwards, intended uses the referrer in which you came from. You shouldn't need to use ->guest()

jrean's avatar

@bashy thank you. Without ->guest() it is not working. Inside trait AuthenticatesAndRegistersUsers and the method postLogin() I tried dd(Session::has('url.intended')) and it returns false.

bashy's avatar

Probably because of a double redirect, have you checked with cURL?

curl -IL http(s)://example.com/some/resource/1
jrean's avatar

@bashy I just did.

I'm logged out. I hit the some/resource/1 and it redirects me to the login route. I complete the form (Laravel defaults), submit and I'm redirected to the /home route.

If I had the ->guest('login') I'm instead redirected to the intended some/resource/1 route.

The output of the curl command is the following:

HTTP/1.1 302 Found
Server: nginx/1.6.2
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Cache-Control: no-cache
Date: Sun, 24 May 2015 15:33:27 GMT
Location: http://xxx.app/login
...

HTTP/1.1 200 OK
Server: nginx/1.6.2
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Cache-Control: no-cache
Date: Sun, 24 May 2015 15:33:27 GMT
...

Please or to participate in this conversation.