1
1
package com.google.firebase.quickstart.auth.kotlin
2
2
3
- import android.app.Activity
4
- import android.content.Intent
5
3
import android.os.Bundle
6
4
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
11
14
import com.google.firebase.auth.FirebaseAuth
12
15
import com.google.firebase.auth.FirebaseUser
13
16
import com.google.firebase.auth.GoogleAuthProvider
14
17
import com.google.firebase.auth.auth
15
18
import com.google.firebase.Firebase
16
19
import com.google.firebase.quickstart.auth.R
20
+ import kotlinx.coroutines.launch
17
21
18
22
/* *
19
23
* Demonstrate Firebase Authentication using a Google ID Token.
20
24
*/
21
- class GoogleSignInActivity : Activity () {
25
+ class GoogleSignInActivity : AppCompatActivity () {
22
26
23
27
// [START declare_auth]
24
28
private lateinit var auth: FirebaseAuth
25
29
// [END declare_auth]
26
30
27
- private lateinit var googleSignInClient: GoogleSignInClient
31
+ // [START declare_credential_manager]
32
+ private lateinit var credentialManager: CredentialManager
33
+ // [END declare_credential_manager]
28
34
29
35
override fun onCreate (savedInstanceState : Bundle ? ) {
30
36
super .onCreate(savedInstanceState)
31
37
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
-
42
38
// [START initialize_auth]
43
39
// Initialize Firebase Auth
44
40
auth = Firebase .auth
45
41
// [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)
46
62
}
47
63
48
64
// [START on_start_check_user]
@@ -54,25 +70,33 @@ class GoogleSignInActivity : Activity() {
54
70
}
55
71
// [END on_start_check_user]
56
72
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 {
64
76
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} " )
72
96
}
73
97
}
74
98
}
75
- // [END onactivityresult ]
99
+ // [END get_user_credential ]
76
100
77
101
// [START auth_with_google]
78
102
private fun firebaseAuthWithGoogle (idToken : String ) {
@@ -93,18 +117,10 @@ class GoogleSignInActivity : Activity() {
93
117
}
94
118
// [END auth_with_google]
95
119
96
- // [START signin]
97
- private fun signIn () {
98
- val signInIntent = googleSignInClient.signInIntent
99
- startActivityForResult(signInIntent, RC_SIGN_IN )
100
- }
101
- // [END signin]
102
-
103
120
private fun updateUI (user : FirebaseUser ? ) {
104
121
}
105
122
106
123
companion object {
107
124
private const val TAG = " GoogleActivity"
108
- private const val RC_SIGN_IN = 9001
109
125
}
110
126
}
0 commit comments