40
40
import com .firebase .ui .auth .ui .ExtraConstants ;
41
41
import com .firebase .ui .auth .ui .FlowParameters ;
42
42
import com .firebase .ui .auth .ui .TaskFailureLogger ;
43
+ import com .firebase .ui .auth .ui .User ;
43
44
import com .google .android .gms .tasks .OnCompleteListener ;
45
+ import com .google .android .gms .tasks .OnFailureListener ;
46
+ import com .google .android .gms .tasks .OnSuccessListener ;
44
47
import com .google .android .gms .tasks .Task ;
45
48
import com .google .firebase .auth .AuthCredential ;
46
49
import com .google .firebase .auth .AuthResult ;
47
50
import com .google .firebase .auth .FacebookAuthProvider ;
48
- import com .google .firebase .auth .FirebaseAuth ;
49
51
import com .google .firebase .auth .FirebaseUser ;
50
52
import com .google .firebase .auth .GoogleAuthProvider ;
51
53
import com .google .firebase .auth .TwitterAuthProvider ;
52
54
53
- public class WelcomeBackIdpPrompt extends AppCompatBase
54
- implements View . OnClickListener , IdpCallback {
55
+ public class WelcomeBackIdpPrompt extends AppCompatBase implements IdpCallback {
56
+ private static final String TAG = "WelcomeBackIdpPrompt" ;
55
57
56
- private static final String TAG = "WelcomeBackIDPPrompt" ;
57
58
private IdpProvider mIdpProvider ;
58
- private IdpResponse mPrevIdpResponse ;
59
59
private AuthCredential mPrevCredential ;
60
60
61
-
62
61
@ Override
63
62
protected void onCreate (Bundle savedInstanceState ) {
64
63
super .onCreate (savedInstanceState );
65
- String providerId = getProviderIdFromIntent ();
66
- mPrevIdpResponse = IdpResponse .fromResultIntent (getIntent ());
67
64
setContentView (R .layout .welcome_back_idp_prompt_layout );
68
65
69
- mIdpProvider = null ;
66
+ IdpResponse newUserIdpResponse = IdpResponse .fromResultIntent (getIntent ());
67
+ mPrevCredential = AuthCredentialHelper .getAuthCredential (newUserIdpResponse );
68
+
69
+ User oldUser = User .getUser (getIntent ());
70
+
71
+ String providerId = oldUser .getProvider ();
70
72
for (IdpConfig idpConfig : mActivityHelper .getFlowParams ().providerInfo ) {
71
73
if (providerId .equals (idpConfig .getProviderId ())) {
72
74
switch (providerId ) {
73
75
case GoogleAuthProvider .PROVIDER_ID :
74
- mIdpProvider = new GoogleProvider (this , idpConfig , getEmailFromIntent ());
76
+ mIdpProvider = new GoogleProvider (this , idpConfig , oldUser . getEmail ());
75
77
break ;
76
78
case FacebookAuthProvider .PROVIDER_ID :
77
79
mIdpProvider = new FacebookProvider (
@@ -89,21 +91,16 @@ protected void onCreate(Bundle savedInstanceState) {
89
91
}
90
92
}
91
93
92
- if (mPrevIdpResponse != null ) {
93
- mPrevCredential = AuthCredentialHelper .getAuthCredential (mPrevIdpResponse );
94
- }
95
-
96
94
if (mIdpProvider == null ) {
97
- getIntent ().putExtra (
98
- ExtraConstants .EXTRA_ERROR_MESSAGE ,
99
- "Firebase login successful." +
100
- " Account linking failed due to provider not enabled by application" );
95
+ Log .w (TAG , "Firebase login unsuccessful."
96
+ + " Account linking failed due to provider not enabled by application: "
97
+ + providerId );
101
98
finish (ResultCodes .CANCELED , IdpResponse .getErrorCodeIntent (ErrorCodes .UNKNOWN_ERROR ));
102
99
return ;
103
100
}
104
101
105
102
((TextView ) findViewById (R .id .welcome_back_idp_prompt ))
106
- .setText (getIdpPromptString (getEmailFromIntent ()));
103
+ .setText (getIdpPromptString (oldUser . getEmail ()));
107
104
108
105
mIdpProvider .setAuthenticationCallback (this );
109
106
findViewById (R .id .welcome_back_idp_button ).setOnClickListener (new OnClickListener () {
@@ -127,83 +124,71 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
127
124
}
128
125
129
126
@ Override
130
- public void onClick (View view ) {
131
- next (mPrevIdpResponse );
132
- }
133
-
134
- @ Override
135
- public void onSuccess (IdpResponse idpResponse ) {
136
- next (idpResponse );
137
- }
138
-
139
- @ Override
140
- public void onFailure (Bundle extra ) {
141
- Toast .makeText (getApplicationContext (), "Error signing in" , Toast .LENGTH_LONG ).show ();
142
- finish (ResultCodes .CANCELED , IdpResponse .getErrorCodeIntent (ErrorCodes .UNKNOWN_ERROR ));
143
- }
144
-
145
- private String getProviderIdFromIntent () {
146
- return getIntent ().getStringExtra (ExtraConstants .EXTRA_PROVIDER );
147
- }
148
-
149
- private String getEmailFromIntent () {
150
- return getIntent ().getStringExtra (ExtraConstants .EXTRA_EMAIL );
151
- }
152
-
153
- private void next (final IdpResponse newIdpResponse ) {
154
- if (newIdpResponse == null ) {
127
+ public void onSuccess (final IdpResponse idpResponse ) {
128
+ if (idpResponse == null ) {
155
129
return ; // do nothing
156
130
}
157
131
158
- AuthCredential newCredential = AuthCredentialHelper .getAuthCredential (newIdpResponse );
132
+ AuthCredential newCredential = AuthCredentialHelper .getAuthCredential (idpResponse );
159
133
if (newCredential == null ) {
160
134
Log .e (TAG , "No credential returned" );
161
135
finish (ResultCodes .CANCELED , IdpResponse .getErrorCodeIntent (ErrorCodes .UNKNOWN_ERROR ));
162
136
return ;
163
137
}
164
138
165
- final FirebaseAuth firebaseAuth = mActivityHelper .getFirebaseAuth ();
166
- FirebaseUser currentUser = firebaseAuth .getCurrentUser ();
167
-
139
+ FirebaseUser currentUser = mActivityHelper .getCurrentUser ();
168
140
if (currentUser == null ) {
169
- Task <AuthResult > authResultTask = firebaseAuth .signInWithCredential (newCredential );
170
- authResultTask .addOnCompleteListener (new OnCompleteListener <AuthResult >() {
171
- @ Override
172
- public void onComplete (@ NonNull Task <AuthResult > task ) {
173
- if (task .isSuccessful () && mPrevCredential != null ) {
174
- FirebaseUser firebaseUser = task .getResult ().getUser ();
175
- firebaseUser .linkWithCredential (mPrevCredential );
176
- firebaseAuth .signOut ();
177
- firebaseAuth
178
- .signInWithCredential (mPrevCredential )
179
- .addOnFailureListener (new TaskFailureLogger (
180
- TAG , "Error signing in with previous credential" ))
181
- .addOnCompleteListener (new FinishListener (newIdpResponse ));
182
- } else {
183
- finish (ResultCodes .OK , IdpResponse .getIntent (newIdpResponse ));
184
- }
185
- }
186
- }).addOnFailureListener (
187
- new TaskFailureLogger (TAG , "Error signing in with new credential" ));
141
+ mActivityHelper .getFirebaseAuth ()
142
+ .signInWithCredential (newCredential )
143
+ .addOnSuccessListener (new OnSuccessListener <AuthResult >() {
144
+ @ Override
145
+ public void onSuccess (AuthResult result ) {
146
+ if (mPrevCredential != null ) {
147
+ result .getUser ()
148
+ .linkWithCredential (mPrevCredential )
149
+ .addOnFailureListener (new TaskFailureLogger (
150
+ TAG , "Error signing in with previous credential" ))
151
+ .addOnCompleteListener (new FinishListener (idpResponse ));
152
+ } else {
153
+ finish (ResultCodes .OK , IdpResponse .getIntent (idpResponse ));
154
+ }
155
+ }
156
+ })
157
+ .addOnFailureListener (new OnFailureListener () {
158
+ @ Override
159
+ public void onFailure (@ NonNull Exception e ) {
160
+ finishWithError ();
161
+ }
162
+ })
163
+ .addOnFailureListener (
164
+ new TaskFailureLogger (TAG , "Error signing in with new credential" ));
188
165
} else {
189
- Task < AuthResult > authResultTask = currentUser . linkWithCredential ( newCredential );
190
- authResultTask
166
+ currentUser
167
+ . linkWithCredential ( newCredential )
191
168
.addOnFailureListener (
192
169
new TaskFailureLogger (TAG , "Error linking with credential" ))
193
- .addOnCompleteListener (new FinishListener (newIdpResponse ));
170
+ .addOnCompleteListener (new FinishListener (idpResponse ));
194
171
}
195
172
}
196
173
174
+ @ Override
175
+ public void onFailure (Bundle extra ) {
176
+ finishWithError ();
177
+ }
178
+
179
+ private void finishWithError () {
180
+ Toast .makeText (this , R .string .general_error , Toast .LENGTH_LONG ).show ();
181
+ finish (ResultCodes .CANCELED , IdpResponse .getErrorCodeIntent (ErrorCodes .UNKNOWN_ERROR ));
182
+ }
183
+
197
184
public static Intent createIntent (
198
185
Context context ,
199
186
FlowParameters flowParams ,
200
- String providerId ,
201
- IdpResponse idpResponse ,
202
- String email ) {
187
+ User existingUser ,
188
+ IdpResponse newUserResponse ) {
203
189
return BaseHelper .createBaseIntent (context , WelcomeBackIdpPrompt .class , flowParams )
204
- .putExtra (ExtraConstants .EXTRA_PROVIDER , providerId )
205
- .putExtra (ExtraConstants .EXTRA_IDP_RESPONSE , idpResponse )
206
- .putExtra (ExtraConstants .EXTRA_EMAIL , email );
190
+ .putExtra (ExtraConstants .EXTRA_USER , existingUser )
191
+ .putExtra (ExtraConstants .EXTRA_IDP_RESPONSE , newUserResponse );
207
192
}
208
193
209
194
private class FinishListener implements OnCompleteListener <AuthResult > {
0 commit comments