AI-generated Key Takeaways
-
SignInClient is an API client for managing user sign-in and sign-up flows.
-
The process involves getting a client instance, initiating sign-in with a request, and handling the result to obtain user credentials.
-
Different BeginSignInRequest configurations are used for signing in an existing user versus signing up a new user.
-
Supporting both Google ID token and password credentials is recommended for signing in existing users to offer more options.
-
Many methods within this client are deprecated and recommend using Credential Manager instead.
A client for the sign-in API.
The Sign-In APIs can be used for both sign-in and sign-up scenarios. The two scenarios
share the same flow in the code, but different BeginSignInRequest
should be provided in different scenarios.
The Sign-In APIs guide the user through credential selection before returning an instance
of SignInCredential
containing the data for sign-in or sign-up.
The recommended process for retrieving credentials using this API is as follows:
- Get a new API client instance by calling
Identity.getSignInClient. - Call
SignInClient.beginSignIn, supplying the constructedBeginSignInRequestas an input. - If the request is successful, at least one matching credential is available. Launch the
PendingIntentfrom theresultof the operation to display the UI that guides the user through sign-in. The result of sign-in will be returned inActivity.onActivityResult; callingSignInClient.getSignInCredentialFromIntentwill either return theSignInCredentialif the operation was successful, or throw anApiExceptionthat indicates the reason for failure. - If the request is unsuccessful, no matching credential was found on the device that can be used to sign the user in. No further action needs to be taken.
When the user signs out of your application, please make sure to call SignInClient.signOut.
The usage of BeginSignInRequest
Different BeginSignInRequest
should be used for sign-in and sign-up.
Sign an existing user in
Two types of credentials are supported in SignInCredential:
Google ID token and password. To give users more options to choose from when selecting a
credential to sign in with, and by extension, increase your app's sign-in rate, it is
strongly recommended that applications support both Google ID token and password
credentials:
- If your application supports username/password login, configure an instance of
PasswordRequestOptionsin the request. - If your application supports federated sign-in using Google ID tokens, configure an
instance of
GoogleIdTokenRequestOptionsaccordingly - be sure to supply your server client ID (you can find this in your Google API console project).For the sign-in scenario, it is strongly recommended to set
GoogleIdTokenRequestOptions.Builder.setFilterByAuthorizedAccountstotrueso only the Google accounts that the user has authorized before will show up in the credential list. This can help prevent a new account being created when the user has an existing account registered with the application.
For example, an app that supports password login and federated sign-in with Google would construct a request as follows:
BeginSignInRequest request = BeginSignInRequest.builder()
.setPasswordRequestOptions(
PasswordRequestOptions.builder()
.setSupported(true)
.build())
.setGoogleIdTokenRequestOptions(
GoogleIdTokenRequestOptions.builder()
.setSupported(true)
// Set filterByAuthorizedAccounts = true to avoid duplicated accounts being created
.setFilterByAuthorizedAccounts(true)
.setServerClientId("serverClientID")
.build())
.build();
Sign up a new user
For the sign-up scenario, only Google ID token credentials should be used. The
GoogleIdTokenRequestOptions may look like the following:
BeginSignInRequest request = BeginSignInRequest.builder()
.setGoogleIdTokenRequestOptions(
GoogleIdTokenRequestOptions.builder()
.setSupported(true)
.setFilterByAuthorizedAccounts(false)
.setServerClientId("serverClientID")
.build())
.build();
Public Method Summary
| abstract Task<BeginSignInResult> |
beginSignIn(BeginSignInRequest
signInRequest)
This method is deprecated. Use Credential Manager
instead.
|
| abstract String |
getPhoneNumberFromIntent(Intent data)
Retrieves the Phone Number from the
Intent
returned upon a successful Phone Number Hint request, throwing an
ApiException if no phone number is available or the input
Intent
is null.
|
| abstract Task<PendingIntent> |
getPhoneNumberHintIntent(GetPhoneNumberHintIntentRequest
getPhoneNumberHintIntentRequest)
Gets the
PendingIntent
that initiates the Phone Number Hint flow.
|
| abstract SignInCredential |
getSignInCredentialFromIntent(Intent data)
This method is deprecated. Use Credential Manager
instead.
|
| abstract Task<PendingIntent> |
getSignInIntent(GetSignInIntentRequest
getSignInIntentRequest)
This method is deprecated. Use Credential Manager
instead.
|
| abstract Task<Void> |
signOut()
This method is deprecated. Use Credential Manager
instead.
|
Public Methods
public abstract Task<BeginSignInResult> beginSignIn (BeginSignInRequest signInRequest)
This method is deprecated.
Use Credential Manager
instead.
Initiates the retrieval of a credential that can assist the caller in signing a user in to their application.
If the request cannot be honored, an exception will be set on the returned
Task. In all
other cases, a BeginSignInResult
will be returned.
Parameters
| signInRequest | configuration for the sign-in operation |
|---|
Returns
Taskwhich eventually contains the result of the initialization
public abstract String getPhoneNumberFromIntent (Intent data)
Retrieves the Phone Number from the Intent
returned upon a successful Phone Number Hint request, throwing an ApiException
if no phone number is available or the input Intent is
null.
Throws
| ApiException |
|---|
public abstract Task<PendingIntent> getPhoneNumberHintIntent (GetPhoneNumberHintIntentRequest getPhoneNumberHintIntentRequest)
Gets the PendingIntent
that initiates the Phone Number Hint flow.
If there is no phone number on the device, an exception will be set on the returned
Task. In all
other cases, a PendingIntent
will be returned.
Returns
Taskwhich can be used to start the Phone Number Hint flow.
public abstract SignInCredential getSignInCredentialFromIntent (Intent data)
This method is deprecated.
Use Credential Manager
instead.
Retrieves the SignInCredential
from the Intent
returned upon successful sign-in, throwing an ApiException
if no credential is present.
Throws
| ApiException |
|---|
public abstract Task<PendingIntent> getSignInIntent (GetSignInIntentRequest getSignInIntentRequest)
This method is deprecated.
Use Credential Manager
instead.
Gets the PendingIntent
that initiates the Google Sign-in flow.
If the request cannot be honored, an exception will be set on the returned
Task. In all
other cases, a PendingIntent
will be returned.
Parameters
| getSignInIntentRequest | configuration for Google Sign-in flow |
|---|
Returns
Taskwhich eventually contains thePendingIntentto start the Google Sign-in flow.
public abstract Task<Void> signOut ()
This method is deprecated.
Use Credential Manager
instead.
Resets internal state related to sign-in.
This method should be invoked when a user signs out of your app.
Returns
Taskwhich eventually terminates in success or failure