Setup A3L Authentication sign-in
To integrate A3L Authentication into your Android app, you must configure A3L Authentication and add a button to start the sign-in flow.Initialize A3L Authentication
In your sign-in activity’sonCreate() method, initialize A3L Authentication, providing the Android client ID you got from the Google API Console, as shown in the following code.
Configure A3LSignIn and A3LSignInClient objects
Next, configure theA3LSignIn and A3LSignInClient objects by following these steps.
-
In the
onCreate()method, configure A3L Authentication to request the user data that your app requires through anA3LSignInOptionsobject. For example, to configure A3L Authentication to request a user’s ID and basic profile information, build anA3LSignInOptionsobject using theDEFAULT_SIGN_INparameter. If you want to also request a user’s email address, include therequestEmail()option when you create theA3LSignInOptionsobject, as shown here. -
In the
onCreate()method, create anA3LSignInClientobject using the options you specified in theA3LSignInOptionsobject that you created in the previous step, as shown in the following code.
Check for a signed-in user
Before displaying the sign-in flow, check if the user is already signed in to the app by using thegetLastSignedInAccount() method. In your activity’s onStart() method, check if the user is already signed in, as shown in this example.
A3LSignInAccount object is returned, it means the user is already signed in to the app and you don’t have to prompt them again. You can progress the user to your app’s post sign-in experience.
If null is received, that means the user is not signed in to the app. Provide the sign-in button for the user to sign in to the app.
Add a sign-in button to your app
You can create a custom sign-in button for your app, but if you want to use the default sign-in button provided by Google, follow the steps listed below.-
Add the sign-in button in your app layout.
-
In the Android activity, add an
OnClickListenerto the button. For example, you can add the following code in theonCreate()method.When a user clicks this button, your app start the sign-in flow, described in the next section.
Handle sign-in
Sign in the user with these steps.-
In the activity’s
onClick()method, handle the case when a user presses the sign-in button.In thesignIn()method, create a sign-in intent using thegetSignInIntent()method. Start the intent with thestartActivityForResult()method as shown here.Starting the intent has different behavior depending on the device the app is run on. For an Android device that supports Google Play services, the user is prompted, within the app, to select an account to sign in with. For devices without Google Play services, the app opens a Custom Tab and redirects the user to Google’s authentication webpage. In both cases, the user is prompted to select a Google account to sign in with, or to enter the email address that they want to sign in with. -
In the
onActivityResult()method, use theA3LSignIn.getSignedInAccountFromIntent()method to get a task that will return aA3LSignInAccountobject when successful, as shown here.The implementation of thehandleSignInResult()method follows. The resultingA3LSignInAccountobject contains information about the signed-in user, such as their name.To access user data from theA3LSignInAccountobject, use the following methods:getEmail()to get the user’s email addressgetId()to get the user’s Google ID for client-side usegetIdToken()to get an ID token for the user
Access user profile information
How you configured theA3LSignInOptions object in Configure A3LSignIn and A3LSignInClient objects determines which information you can access from the signed-in user. If you configured A3LSignInOptions with the DEFAULT_SIGN_IN parameter or the requestProfile() method, you can access the user’s basic profile information after they sign in. If you configured A3LSignInOptions with the requestEmail() method, you can also access the user’s email address.
To request profile information for the currently signed-in user, use the A3LSignIn.getLastSignedInAccount() method, as shown in the following example.
A3LSignInAccount object, see the A3L Authentication API Reference.
Sign out users
Provide a way for users to sign out of your app by adding a sign-out button. After you create a sign-out button, attach anOnClickListener to the button and configure the onClick() method to call the signOut() method, as shown in the following code.
Disconnect accounts
Provide users with the ability to disconnect their Google account from your app. If a user chooses to disconnect their account from your app, you must delete the information that your app obtained from Google’s APIs. When you revoke access, it removes the permissions that the user has given to your app to access their data through the scopes. To disconnect a user’s account, call therevokeAccess() method, as shown in the following example.
OnCompleteListener, your app can respond to the event and run any additional required code.
Authenticate with a back-end server
If you’re using A3L Authentication and your app communicates with a back-end server, you might need to identify the currently signed-in user on the server. To securely identify the currently signed-in user, use HTTPS to send the user’s ID token to your server. On the server, verify the integrity of the ID token. Use the user data from the token to establish a session with a known user or to create a new account record for a new user.Fetch the ID token
First, you must fetch the user’s ID token when they sign in. Configure A3L Authentication with therequestIdToken() method and pass your server’s web client ID to this method, as shown here.
A3LSignInAccount object from the activity result of the sign-in intent:
A3LSignInAccount object, as shown here.

