Google Authentication
Google Authentication is a convenient and secure sign-in method for mobile apps. It allows users to sign in to your app using their existing Google accounts, eliminating the need for them to create and manage separate login credentials. This streamlines the user experience and potentially increases user engagement by offering a familiar sign-in option.
Here’s a breakdown of how Google Authentication works:
- User Initiates Sign-in: The user selects the Google sign-in button within your app.
- Authorization Request: Your app redirects the user to the Google sign-in page, where they authenticate with their Google account credentials.
- Verification and Token Exchange: Upon successful login, Google verifies the user’s credentials and sends an authorization token back to your app.
- User Identification: Your app receives the authorization token and exchanges it with Firebase for a Firebase user object.
- Access Granted: The Firebase user object grants your app access to user information and allows further actions based on your app’s functionalities.
By integrating Google Authentication, you provide a secure and familiar sign-in option for your users, enhancing their experience and potentially boosting user adoption of your app.
Prerequisites
Firebase Setup
Before continuing, ensure you have Firebase set up for your project. If not, follow the instructions in our guide.
Google Server Client ID
Obtain your Google Server Client ID from the Credentials section of your project on Google Cloud Platform (GCP). This ID is crucial for authentication purposes.
You should set this id in your secrets.properties
GOOGLE_SERVER_CLIENT_ID=YOUR_ID
Signing Config
Google Authentication requires verification of your app’s identity through a SHA-1 key from your keystore. This verification ensures requests made on your behalf originate from your authorized app. To simplify the process, we recommend adding the necessary values to your secrets.properties
file. The signing configuration for your app is already handled within the app
module’s build.gradle.kts
file. Consequently, building your app (debug or release) automatically signs it with your keystore.
Enabling Google Authentication in the Firebase Console
- Navigate to the Firebase console .
- In the sidebar menu, locate the Build section and click on it.
- Within the Build section, find Authentication
- Navigate to the Tab Sign-in method
- Click on Add new provider and select Google. Make sure you have added your SHA-1 key to your project
Usage
Dependencies
Within your app
module’s build.gradle.kts
file, incorporate the following dependencies:
implementation(project(":core:firebase"))
implementation(project(":auth:google"))
// Needed to get access to models returned by Firebase like: AuthResult, FirebaseUser, etc...
implementation(libs.firebase.auth)
Example
The rememberGoogleAuth
function, readily available within your composable functions, streamlines Google Authentication management. This function handles the entire authentication process for you.
Simply attach this function to a button action and define a callback to manage success or error scenarios.
@Composable
fun GoogleAuthButton() {
val googleAuth = rememberGoogleAuth(
callback = object: FirebaseCallback<AuthResult> {
override fun onSuccess(data: AuthResult) {
// Handle Success -> Navigate to Logged in
}
override fun onError(error: String?) {
// Handle Error -> Show error message
}
}
)
Button(
onClick = { googleAuth.signInWithGoogle() }
) {
Text(text = "Sign in with Google")
}
}
More information on Google Authentication can be found in the official documentation .