Skip to content

Commit c094f3a

Browse files
committed
Add Credential Manager to Kotlin sample
1 parent a36b9e5 commit c094f3a

File tree

2 files changed

+62
-41
lines changed

2 files changed

+62
-41
lines changed

auth/app/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ dependencies {
4949
// [START gradle_firebase_ui_auth]
5050
implementation("com.firebaseui:firebase-ui-auth:8.0.2")
5151

52+
// Google Identity Services SDK (only required for Auth with Google)
53+
implementation("androidx.credentials:credentials:1.3.0")
54+
implementation("androidx.credentials:credentials-play-services-auth:1.3.0")
55+
implementation("com.google.android.libraries.identity.googleid:googleid:1.1.1")
56+
5257
// Required only if Facebook login support is required
5358
// Find the latest Facebook SDK releases here: https://goo.gl/Ce5L94
5459
implementation("com.facebook.android:facebook-android-sdk:4.42.0")
Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,64 @@
11
package com.google.firebase.quickstart.auth.kotlin
22

3-
import android.app.Activity
4-
import android.content.Intent
53
import android.os.Bundle
64
import android.util.Log
7-
import com.google.android.gms.auth.api.signin.GoogleSignIn
8-
import com.google.android.gms.auth.api.signin.GoogleSignInClient
9-
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
10-
import com.google.android.gms.common.api.ApiException
5+
import androidx.appcompat.app.AppCompatActivity
6+
import androidx.credentials.CredentialManager
7+
import androidx.credentials.CustomCredential
8+
import androidx.credentials.GetCredentialRequest
9+
import androidx.credentials.exceptions.GetCredentialException
10+
import androidx.lifecycle.lifecycleScope
11+
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
12+
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
13+
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential.Companion.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL
1114
import com.google.firebase.auth.FirebaseAuth
1215
import com.google.firebase.auth.FirebaseUser
1316
import com.google.firebase.auth.GoogleAuthProvider
1417
import com.google.firebase.auth.auth
1518
import com.google.firebase.Firebase
1619
import com.google.firebase.quickstart.auth.R
20+
import kotlinx.coroutines.launch
1721

1822
/**
1923
* Demonstrate Firebase Authentication using a Google ID Token.
2024
*/
21-
class GoogleSignInActivity : Activity() {
25+
class GoogleSignInActivity : AppCompatActivity() {
2226

2327
// [START declare_auth]
2428
private lateinit var auth: FirebaseAuth
2529
// [END declare_auth]
2630

27-
private lateinit var googleSignInClient: GoogleSignInClient
31+
// [START declare_credential_manager]
32+
private lateinit var credentialManager: CredentialManager
33+
// [END declare_credential_manager]
2834

2935
override fun onCreate(savedInstanceState: Bundle?) {
3036
super.onCreate(savedInstanceState)
3137

32-
// [START config_signin]
33-
// Configure Google Sign In
34-
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
35-
.requestIdToken(getString(R.string.default_web_client_id))
36-
.requestEmail()
37-
.build()
38-
39-
googleSignInClient = GoogleSignIn.getClient(this, gso)
40-
// [END config_signin]
41-
4238
// [START initialize_auth]
4339
// Initialize Firebase Auth
4440
auth = Firebase.auth
4541
// [END initialize_auth]
42+
43+
// [START initialize_credential_manager]
44+
// Initialize Credential Manager
45+
credentialManager = CredentialManager.create(baseContext)
46+
// [END initialize_credential_manager]
47+
48+
// [START configure_credential_manager]
49+
// Create the configuration for the Credential Manager request
50+
val googleIdOption = GetGoogleIdOption.Builder()
51+
.setFilterByAuthorizedAccounts(true)
52+
.setServerClientId(baseContext.getString(R.string.default_web_client_id))
53+
.build()
54+
55+
// Create the Credential Manager request using the configuration created above
56+
val request = GetCredentialRequest.Builder()
57+
.addCredentialOption(googleIdOption)
58+
.build()
59+
// [END configure_credential_manager]
60+
61+
getCredential(request)
4662
}
4763

4864
// [START on_start_check_user]
@@ -54,25 +70,33 @@ class GoogleSignInActivity : Activity() {
5470
}
5571
// [END on_start_check_user]
5672

57-
// [START onactivityresult]
58-
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
59-
super.onActivityResult(requestCode, resultCode, data)
60-
61-
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
62-
if (requestCode == RC_SIGN_IN) {
63-
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
73+
// [START get_user_credential]
74+
private fun getCredential(request: GetCredentialRequest) {
75+
lifecycleScope.launch {
6476
try {
65-
// Google Sign In was successful, authenticate with Firebase
66-
val account = task.getResult(ApiException::class.java)!!
67-
Log.d(TAG, "firebaseAuthWithGoogle:" + account.id)
68-
firebaseAuthWithGoogle(account.idToken!!)
69-
} catch (e: ApiException) {
70-
// Google Sign In failed, update UI appropriately
71-
Log.w(TAG, "Google sign in failed", e)
77+
// Launch Credential Manager UI
78+
val result = credentialManager.getCredential(
79+
request = request,
80+
context = baseContext
81+
)
82+
83+
// Extract credential from the result returned by Credential Manager
84+
val credential = result.credential
85+
86+
// Check if credential is of type Google ID
87+
if (credential is CustomCredential && credential.type == TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
88+
val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.data)
89+
// Sign in to Firebase with the Google ID Token
90+
firebaseAuthWithGoogle(googleIdTokenCredential.idToken)
91+
} else {
92+
Log.d(TAG, "Credential is not of type Google ID!")
93+
}
94+
} catch (e: GetCredentialException) {
95+
Log.e(TAG, "Couldn't retrieve user's credentials: ${e.localizedMessage}")
7296
}
7397
}
7498
}
75-
// [END onactivityresult]
99+
// [END get_user_credential]
76100

77101
// [START auth_with_google]
78102
private fun firebaseAuthWithGoogle(idToken: String) {
@@ -93,18 +117,10 @@ class GoogleSignInActivity : Activity() {
93117
}
94118
// [END auth_with_google]
95119

96-
// [START signin]
97-
private fun signIn() {
98-
val signInIntent = googleSignInClient.signInIntent
99-
startActivityForResult(signInIntent, RC_SIGN_IN)
100-
}
101-
// [END signin]
102-
103120
private fun updateUI(user: FirebaseUser?) {
104121
}
105122

106123
companion object {
107124
private const val TAG = "GoogleActivity"
108-
private const val RC_SIGN_IN = 9001
109125
}
110126
}

0 commit comments

Comments
 (0)