Full Stack Alien
Full Stack Alien

Reputation: 12501

Google API: Can I authenticate a specific user with PHP Client?

Users in my app are connecting their Gmail accounts. I have a feature that allows them to disconnect and then before totally deleting the integration and all the data associated with it they can reconnect. I'm using OAuth 2 authentication method.

Currently, when I authenticating back with Google the sign-in screen is presented and if that user has multiple Gmail logins then they are all presented to the user to choose from.

If the user chooses a different email than they chose before this creates a conflict in my system. For this specific scenario, I need to choose that email for them. Is this possible?

Upvotes: 0

Views: 528

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

There is no way to 100% ensure that they will login with the same email address however you can add

    $client->setLoginHint('[Users email here]');

This will encourage them to login with that email as the popup window where they have to select a google account will only display that one. This will require that you have saved the email from the last time.

function buildClient(){

    $client = new Google_Client();
    $client->setAccessType("offline");        // offline access.  Will result in a refresh token
    $client->setIncludeGrantedScopes(true);   // incremental auth
    $client->setAuthConfig(__DIR__ . '/client_secrets.json');
    $client->addScope([YOUR SCOPES HERE]);
    $client->setLoginHint('[Users email here]');
    $client->setRedirectUri(getRedirectUri());  
    return $client;
}

Extra info from the docs

openidconnect documentation

enter image description here

Upvotes: 1

Related Questions