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

ImtiazEpu's avatar

Laravel redirect()->intended() is not working in custom login method

I am currently developing Cart page of my e-commerce application, If I click on checkout button it will redirect Login page, After login, I need to go to the Checkout page or cart page, But Any time I logged in It will redirect me to Home page. Is there Any easy way to do that in Laravel?

public function processLogin()
    {
        $validator = Validator::make(request()->all(), [
            'email' => 'required|email',
            'password' => 'required',
        ]);
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $credentials = request()->only(['email', 'password']);
        if(auth()->attempt($credentials)) {
            if (Auth::user()->hasAnyRole('admin')){
                alert()->success('Success', 'Admin logged in!')->toToast();
                return redirect('/dashboard');
            } else {
                if (auth()->user()->email_verified_at === null) {
                    auth()->logout();
                    alert()->warning('Pending', 'Your account is not activated!')->toToast();
                    return redirect()->route('login');
                }
            }
            alert()->success('Success', auth()->user()->name.' logged in!')->toToast();
            return redirect()->intended();
        }
    
        alert()->error('Invalid credentials', 'Your Email or password was incorrect!');
        return redirect()->back();
    }
0 likes
7 replies
mstrauss's avatar

How about this?

    public function processLogin()
    {
        $validator = Validator::make(request()->all(), [
            'email' => 'required|email',
            'password' => 'required',
        ]);
        
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $credentials = request()->only(['email', 'password']);
        if(auth()->attempt($credentials)) {
            if (Auth::user()->hasAnyRole('admin'))
                alert()->success('Success', 'Admin logged in!')->toToast();
            return redirect('/dashboard');
        } else {
            if (auth()->user()->email_verified_at === null) {
                auth()->logout();
                alert()->warning('Pending', 'Your account is not activated!')->toToast();
                return redirect()->route('login');
            }
        }
        
        alert()->success('Success', auth()->user()->name.' logged in!')->toToast();
        return redirect()->intended();
        
    }

Your original code has some issues with the closing brackets and unreachable return statements. I'm surprised your IDE isn't squawking about it. Please note, I removed the final return statement as it was unreachable and redundant.

Also take a look at the below if/else statement from your example. Essentially you are saying, if the credentials are valid and the user is an admin, then send them to /dashboard.

        if(auth()->attempt($credentials)) {
            if (Auth::user()->hasAnyRole('admin'))
                alert()->success('Success', 'Admin logged in!')->toToast();
            return redirect('/dashboard');
        } else {
            if (auth()->user()->email_verified_at === null) {
                auth()->logout();
                alert()->warning('Pending', 'Your account is not activated!')->toToast();
                return redirect()->route('login');
            }
        }

But if the credentials are invalid and the user's email as not yet been verified, then redirect them to the login page with the message that the account has not been activated. But then, and this is important, if the credentials are not valid, the following block is executed.

 alert()->success('Success', auth()->user()->name.' logged in!')->toToast();
        return redirect()->intended();

I guess that's why you originally had the final return statement, below, in the original code. But I'm pretty sure that was unreachable in the structure you had used.

alert()->error('Invalid credentials', 'Your Email or password was incorrect!');
return redirect()->back();

Anyway, good luck with the project!

1 like
ImtiazEpu's avatar

Thanks for replay. I have some mistake in posting this code. Thanks, I checked my code again & I found the starting bracket is missing. if (Auth::user()->hasAnyRole('admin')){

Thank for your effort. But I'm still stuck with an intended issue.

mstrauss's avatar

Okay, just so I understand the scenario:

  • A site guest (unauthenticated user) clicks the checkout link
  • And they are then routed to the site login page
  • They enter their login credentials, which are then passed to your processLogin method

How do you redirect them to the login page when the guest attempts to go to the checkout URL? You have to use the guest method on the Redirect class, as this sets the intendedUrl. So something like: return redirect()->guest('/login'))

See the Laravel methods below to understand this a bit better:

Redirector Class

    public function guest($path, $status = 302, $headers = [], $secure = null)
    {
        $request = $this->generator->getRequest();

        $intended = $request->method() === 'GET' && $request->route() && ! $request->expectsJson()
                        ? $this->generator->full()
                        : $this->generator->previous();

        if ($intended) {
            $this->setIntendedUrl($intended);
        }

       
    public function intended($default = '/', $status = 302, $headers = [], $secure = null)
    {
        $path = $this->session->pull('url.intended', $default);

        return $this->to($path, $status, $headers, $secure);
    }

1 like
siangboon's avatar

perhaps you could look at App/Middleware/RedirectIfAuthenticated.php and try from there.

ImtiazEpu's avatar
ImtiazEpu
OP
Best Answer
Level 3

Thank you guys for your help. The problem has been solved. I just use auth middleware on my checkout route.

Please or to participate in this conversation.