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

muragijimana's avatar

Get Login

i am posting a login form with ajax

$.ajax({ type: "POST", url: "/auth/login", data: "email=beastar457@gmail.com&password=12345", success: function(msg){ window.location.replace("/In/strims"); }, error: function(XMLHttpRequest, textStatus, errorThrown) { window.location.replace("/"); } }); shakeModal() /* Simulate error message from the server */

} in my trait AuthenticatesAndRegistersUsers i changed to :

public function postLogin(Request $request) { $this->validate($request, [ 'email' => 'required|email', 'password' => 'required', ]);

    $credentials = $request->only('email', 'password');

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

}

ant the problem is that when it fails it redirect me back to /auth/login how can i avoid it?

0 likes
4 replies
bimalshah72's avatar

I think instead of commenting // return redirect()->intended($this->redirectPath());

Add protected property in AuthController

protected $redirectTo = 'your path goes here...';

muragijimana's avatar

i think that it is because of that url i am passing when i am posting a login and i am wondering how can i handle that in

$.ajax({ type: "POST", url: "/auth/login", data: "email=beastar457@gmail.com&password=12345", success: function(msg){ window.location.replace("/In/strims"); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert('the path when error arise'); } });

that is the login i have but not working even when i change to

$.ajax({ type: "POST", url: "/auth/login", data: "email=beastar457@gmail.com&password=12345", success: function(msg){ window.location.replace("/In/strims"); }, error: function(XMLHttpRequest, textStatus, errorThrown) { window.location.replace("/"); } });

bimalshah72's avatar

@muragijimana

what if you keep

public function postLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email', 'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');

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

        return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors([
                        'email' => $this->getFailedLoginMessage(),
                    ]);
    }

as it is and add following in AuthController?

$protected $loginPath = '/';

hence when it fails, it executes,

return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors([
                        'email' => $this->getFailedLoginMessage(),
                    ]);

and loginPath now would be '/'

Please or to participate in this conversation.