Search code examples
androidcredentialsgoogle-plus-signingoogle-smartlockpasswords

Can't save IdentityProviders.GOOGLE credentials with SmartLock


I'm logging my users into our app using Google+. The actual G+ signing in happens on the server though. I get back some user data such as email and name. I'm using this to store the users credentials as a google account.

The issue arises when I try to save it. In the callback to save the creds in "Auth.CredentialsApi.save" it returns no resolution and nothing ends up happening. If I remove the Google IdentityProvider from the creds then it saves without a password.

Does anyone know why a resolution is not found when the IndentityProvider is set?


Solution

  • In Play Services 8+, no resolution (confirmation UI) is required to save a Google Sign-In to the user's Google Account using the Smart Lock API (i.e., the credential's type is set to IdentityProviders.GOOGLE and the email address matches one signed in on the device, and the user has not disabled saving in settings). Note that you cannot set both an account type and password on a credential object when saving with the API -- if a user has a password simply store that for authentication purposes.

    An API call with a credential of this type should save automatically and be available immediately, so just check that this credential is available when making a Auth.CredentialsApi.request() using a CredentialRequest built with .setAccountTypes(IdentityProviders.GOOGLE) and that the saved credential is shown on passwords.google.com and has "with Google" in place of a password.

    When retrieving this credential, you can use it to know that you've got a user with an existing account and they signed in with Google previously. You then can customize the auth UI for this returning user, or simply trigger a Google Sign-In flow for the user automatically and give them a returning user experience when the app starts using the Auth.GoogleSignInApi.silentSignIn() method in the latest version of the Google Sign-In library. Here's a full code example.