Search code examples
javascriptlaravellaravel-socialite

How to prevent redirection when using auth()->login() method in laravel?


I am trying to use Laravel socialte plugin to login user using popup. So, during callback I am using scripts to call parent window function. But, its never called because, auth()->login() function redirects to home page as soon as its called. How do I stop this redirection?

public function handleProviderCallback($provider)
{
    $user = $this->createOrGetUser($provider);
    echo "<script>
    var Parent = window.opener;</script>";
    if(auth()->login($user)) {
        $tempuser = json_encode($user);
        $script = "<script>Parent.oauthSuccess(true,'$tempuser');</script>";
    } else {
        $script = "<script>Parent.oauthSuccess(false);</script>";
    }
    return $script;
    // $user->token;
}

Solution

  • You can manually login the user:

    public function authenticate(Request $request)
    {
        $credentials = $request->only('email', 'password');
    
        if (Auth::attempt($credentials)) {
            // Authentication passed...do something
        }
    }
    

    Docs: https://laravel.com/docs/5.7/authentication#authenticating-users