Wrong redirect after success login

292 Views Asked by At

I have an application that has been working for several years. Login worked as it should, i.e. after redirection to Google login, access granted. The user was redirected to the address provided via setRedirectUri. I have been having a problem for a few weeks because suddenly, after successful login, the user is redirected to myaccount.google.com despite no modifications to the code.

I am attaching a code snippet

$credentials_file = $parameterBag->get('google_credentials');

$client = new Google_Client();
$client->setAuthConfig($credentials_file);

$client->setAccessType("offline");
$client->setIncludeGrantedScopes(true);

$client->setRedirectUri( $this->router->generate('oauth2callback', [], UrlGeneratorInterface::ABSOLUTE_URL) );
$client->setScopes(self::SCOPES);
$client->setPrompt('select_account consent');
$client->setApprovalPrompt('auto');

$code = $request->get('code', null);

if ( !$code ) {

    $auth_url = $client->createAuthUrl();
    return $this->redirect($auth_url);

} else {
    //magic with logged user
}
2

There are 2 best solutions below

2
On
  1. Have you setup your redirect url in the google admin panel.
  2. If you are using a local website try using http://localhost as your redirect domain and something like http://localhost/authorize for the redirect url and see if this works first, then change it afterwards if you need to.

google redirect url

2
On

Pay very close attention to the Authorized redirect URI in your client ID settings (because this is a web server request you don't need to bother with the javascript origin)

enter image description here

The protocol, the host, the port and the path all need to match exactly what you are sending, any difference would result in google dropping the redirect

I see from your comment the redirect uri you are sending is https://127.0.0.1:8000/oauth2callback

Make sure it is exactly what you have in this setting

If it's not that would explain the problem, if you don't have https configured locally then pay attention to the config of your app because it seems to generate a https url