1
+ package com.firebase.ui.auth.ui.email
2
+
3
+ import android.app.Activity
4
+ import android.app.Application
5
+ import android.app.PendingIntent
6
+ import android.content.Intent
7
+ import android.util.Log
8
+ import com.firebase.ui.auth.data.model.PendingIntentRequiredException
9
+ import com.firebase.ui.auth.data.model.Resource
10
+ import com.firebase.ui.auth.data.model.User
11
+ import com.firebase.ui.auth.util.data.ProviderUtils
12
+ import com.firebase.ui.auth.viewmodel.AuthViewModelBase
13
+ import com.firebase.ui.auth.viewmodel.RequestCodes
14
+ import com.google.android.gms.auth.api.identity.BeginSignInRequest
15
+ import com.google.android.gms.auth.api.identity.Identity
16
+ import com.google.android.gms.auth.api.identity.SignInCredential
17
+ import com.google.android.gms.auth.api.identity.SignInClient
18
+ import com.google.android.gms.common.api.ApiException
19
+ import androidx.annotation.Nullable
20
+
21
+ class CheckEmailHandler (application : Application ) : AuthViewModelBase<User>(application) {
22
+ companion object {
23
+ private const val TAG = " CheckEmailHandler"
24
+ }
25
+
26
+ /* *
27
+ * Initiates a hint picker flow using the new Identity API.
28
+ * This replaces the deprecated Credentials API call.
29
+ */
30
+ fun fetchCredential () {
31
+ val signInClient: SignInClient = Identity .getSignInClient(getApplication())
32
+ val signInRequest = BeginSignInRequest .builder()
33
+ .setPasswordRequestOptions(
34
+ BeginSignInRequest .PasswordRequestOptions .builder()
35
+ .setSupported(true )
36
+ .build()
37
+ )
38
+ .build()
39
+
40
+ signInClient.beginSignIn(signInRequest)
41
+ .addOnSuccessListener { result ->
42
+ // The new API returns a PendingIntent to launch the hint picker.
43
+ val pendingIntent: PendingIntent = result.pendingIntent
44
+ setResult(
45
+ Resource .forFailure(
46
+ PendingIntentRequiredException (pendingIntent, RequestCodes .CRED_HINT )
47
+ )
48
+ )
49
+ }
50
+ .addOnFailureListener { e ->
51
+ Log .e(TAG , " beginSignIn failed" , e)
52
+ setResult(Resource .forFailure(e))
53
+ }
54
+ }
55
+
56
+ /* *
57
+ * Fetches the top provider for the given email.
58
+ */
59
+ fun fetchProvider (email : String ) {
60
+ setResult(Resource .forLoading())
61
+ ProviderUtils .fetchTopProvider(getAuth(), getArguments(), email)
62
+ .addOnCompleteListener { task ->
63
+ if (task.isSuccessful) {
64
+ setResult(Resource .forSuccess(User .Builder (task.result, email).build()))
65
+ } else {
66
+ setResult(Resource .forFailure(task.exception ? : Exception (" Unknown error" )))
67
+ }
68
+ }
69
+ }
70
+
71
+ /* *
72
+ * Handles the result from the hint picker launched via the new Identity API.
73
+ */
74
+ fun onActivityResult (requestCode : Int , resultCode : Int , @Nullable data : Intent ? ) {
75
+ if (requestCode != RequestCodes .CRED_HINT || resultCode != Activity .RESULT_OK ) {
76
+ return
77
+ }
78
+
79
+ setResult(Resource .forLoading())
80
+ val signInClient: SignInClient = Identity .getSignInClient(getApplication())
81
+ try {
82
+ // Retrieve the SignInCredential from the returned intent.
83
+ val credential: SignInCredential = signInClient.getSignInCredentialFromIntent(data)
84
+ val email: String = credential.id
85
+
86
+ ProviderUtils .fetchTopProvider(getAuth(), getArguments(), email)
87
+ .addOnCompleteListener { task ->
88
+ if (task.isSuccessful) {
89
+ setResult(
90
+ Resource .forSuccess(
91
+ User .Builder (task.result, email)
92
+ .setName(credential.displayName)
93
+ .setPhotoUri(credential.profilePictureUri)
94
+ .build()
95
+ )
96
+ )
97
+ } else {
98
+ setResult(Resource .forFailure(task.exception ? : Exception (" Unknown error" )))
99
+ }
100
+ }
101
+ } catch (e: ApiException ) {
102
+ Log .e(TAG , " getSignInCredentialFromIntent failed" , e)
103
+ setResult(Resource .forFailure(e))
104
+ }
105
+ }
106
+ }
0 commit comments