66import android .util .Log ;
77
88import com .firebase .ui .auth .AuthUI ;
9+ import com .firebase .ui .auth .ErrorCodes ;
10+ import com .firebase .ui .auth .IdpResponse ;
911import com .google .android .gms .tasks .OnCompleteListener ;
1012import com .google .android .gms .tasks .OnFailureListener ;
1113import com .google .android .gms .tasks .OnSuccessListener ;
1214import com .google .android .gms .tasks .Task ;
15+ import com .google .firebase .auth .AuthCredential ;
1316import com .google .firebase .auth .AuthResult ;
1417import com .google .firebase .auth .FirebaseAuth ;
1518import com .google .firebase .auth .FirebaseUser ;
2831
2932import static android .app .Activity .RESULT_OK ;
3033
31- public class FirebaseUIAuthPlugin extends CordovaPlugin implements OnCompleteListener <AuthResult >, FirebaseAuth . AuthStateListener {
34+ public class FirebaseUIAuthPlugin extends CordovaPlugin implements OnCompleteListener <AuthResult > {
3235 private static final int RC_SIGN_IN = 123 ;
3336 private final String TAG = "FirebaseUIAuthPlugin" ;
3437 List <AuthUI .IdpConfig > providers = Arrays .asList (
35- new AuthUI .IdpConfig .Builder ( AuthUI . EMAIL_PROVIDER ).build ());
38+ new AuthUI .IdpConfig .EmailBuilder ( ).build ());
3639 private CallbackContext callbackContext ;
3740 private FirebaseAuth firebaseAuth ;
3841 private JSONObject options ;
@@ -51,6 +54,8 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
5154 return initialise (args , callbackContext );
5255 } else if ("signIn" .equals (action )) {
5356 return signIn (callbackContext );
57+ } else if ("signInAnonymously" .equals (action )) {
58+ return signInAnonymously (callbackContext );
5459 } else if ("signOut" .equals (action )) {
5560 return signOut (callbackContext );
5661 } else if ("deleteUser" .equals (action )) {
@@ -84,6 +89,13 @@ public void onSuccess(Void aVoid) {
8489 return true ;
8590 }
8691
92+ private boolean signInAnonymously (CallbackContext callbackContext ) {
93+
94+ signInAnonymous (firebaseAuth );
95+
96+ return true ;
97+ }
98+
8799 private boolean sendEmailVerification (final CallbackContext callbackContext ) {
88100
89101 FirebaseUser user = firebaseAuth .getCurrentUser ();
@@ -127,14 +139,14 @@ private boolean initialise(JSONArray args, final CallbackContext callbackContext
127139
128140 firebaseAuth = FirebaseAuth .getInstance ();
129141
130- firebaseAuth .addAuthStateListener (this );
131- this .signInAnonymous (firebaseAuth );
132-
133142 return true ;
134143 }
135144
136145 private void signInAnonymous (final FirebaseAuth firebaseAuth ) {
137- if (firebaseAuth .getCurrentUser () == null && anonymous ) {
146+
147+ FirebaseUser user = firebaseAuth .getCurrentUser ();
148+
149+ if (user == null && anonymous ) {
138150 firebaseAuth .signInAnonymously ().addOnCompleteListener (new OnCompleteListener <AuthResult >() {
139151 @ Override
140152 public void onComplete (@ NonNull Task <AuthResult > task ) {
@@ -167,19 +179,25 @@ private void createProviderList() throws JSONException {
167179 Log .d (TAG , "createProviderList: parsing provider " + providerList .getString (i ));
168180
169181 if ("EMAIL" .equals (providerList .getString (i ))) {
170- providers .add (new AuthUI .IdpConfig .Builder ( AuthUI . EMAIL_PROVIDER ).build ());
182+ providers .add (new AuthUI .IdpConfig .EmailBuilder ( ).build ());
171183 }
172184 if ("FACEBOOK" .equals (providerList .getString (i ))) {
173- providers .add (new AuthUI .IdpConfig .Builder ( AuthUI . FACEBOOK_PROVIDER ).build ());
185+ providers .add (new AuthUI .IdpConfig .FacebookBuilder ( ).build ());
174186 }
175187 if ("GOOGLE" .equals (providerList .getString (i ))) {
176- providers .add (new AuthUI .IdpConfig .Builder ( AuthUI . GOOGLE_PROVIDER ).build ());
188+ providers .add (new AuthUI .IdpConfig .GoogleBuilder ( ).build ());
177189 }
178190 if ("PHONE" .equals (providerList .getString (i ))) {
179- providers .add (new AuthUI .IdpConfig .Builder ( AuthUI . PHONE_VERIFICATION_PROVIDER ).build ());
191+ providers .add (new AuthUI .IdpConfig .PhoneBuilder ( ).build ());
180192 }
181193 if ("TWITTER" .equals (providerList .getString (i ))) {
182- providers .add (new AuthUI .IdpConfig .Builder (AuthUI .TWITTER_PROVIDER ).build ());
194+ providers .add (new AuthUI .IdpConfig .TwitterBuilder ().build ());
195+ }
196+ if ("GITHUB" .equals (providerList .getString (i ))) {
197+ providers .add (new AuthUI .IdpConfig .GitHubBuilder ().build ());
198+ }
199+ if ("ANONYMOUS" .equals (providerList .getString (i ))) {
200+ providers .add (new AuthUI .IdpConfig .AnonymousBuilder ().build ());
183201 }
184202 }
185203 }
@@ -199,8 +217,11 @@ private boolean signIn(CallbackContext callbackContext) {
199217 } else {
200218 Log .d (TAG , "signIn: sign in" );
201219
220+ cordova .setActivityResultCallback (this );
221+
202222 cordova .getActivity ().startActivityForResult (
203223 this .buildCustomInstance ()
224+ .enableAnonymousUsersAutoUpgrade ()
204225 .setAvailableProviders (providers )
205226 .build (),
206227 RC_SIGN_IN );
@@ -220,7 +241,7 @@ private AuthUI.SignInIntentBuilder buildCustomInstance() {
220241 try {
221242 if (options .has ("logo" )) {
222243 int id = getIdentifier (options .getString ("logo" ), "drawable" );
223- instance = instance .setLogo (id );
244+ // instance = instance.setLogo(id);
224245 }
225246 if (options .has ("theme" )) {
226247 int id = getIdentifier (options .getString ("theme" ), "style" );
@@ -229,11 +250,15 @@ private AuthUI.SignInIntentBuilder buildCustomInstance() {
229250 int id = getIdentifier ("FirebaseUILogonTheme" , "style" );
230251 instance = instance .setTheme (id );
231252 }
232- if (options .has ("tosUrl" )) {
233- instance = instance .setTosUrl (options .getString ("tosUrl" ));
234- }
235- if (options .has ("privacyPolicyUrl" )) {
236- instance = instance .setPrivacyPolicyUrl (options .getString ("privacyPolicyUrl" ));
253+ if (options .has ("tosUrl" ) && options .has ("privacyPolicyUrl" )) {
254+ instance = instance .setTosAndPrivacyPolicyUrls (options .getString ("tosUrl" ),options .getString ("privacyPolicyUrl" ));
255+ } else {
256+ if (options .has ("tosUrl" )) {
257+ instance = instance .setTosUrl (options .getString ("tosUrl" ));
258+ }
259+ if (options .has ("privacyPolicyUrl" )) {
260+ instance = instance .setPrivacyPolicyUrl (options .getString ("privacyPolicyUrl" ));
261+ }
237262 }
238263 if (options .has ("smartLockEnabled" )) {
239264 smartLockEnabled = options .getBoolean ("smartLockEnabled" );
@@ -255,14 +280,81 @@ private int getIdentifier(String name, String type) {
255280 cordova .getActivity ().getApplicationContext ().getPackageName ());
256281 }
257282
283+ @ Override
284+ public void onActivityResult (int requestCode , int resultCode , Intent data ) {
285+ super .onActivityResult (requestCode , resultCode , data );
286+
287+ Log .d (TAG , "onActivityResult" );
288+ Log .d (TAG , "onActivityResult:requestCode:" + requestCode );
289+
290+ if (requestCode == RC_SIGN_IN ) {
291+ Log .d (TAG , "onActivityResult:resultCode:" + resultCode );
292+
293+ IdpResponse response = IdpResponse .fromResultIntent (data );
294+
295+ if (resultCode == RESULT_OK ) {
296+
297+ FirebaseUser user = FirebaseAuth .getInstance ().getCurrentUser ();
298+ raiseEventForUser (user );
299+ } else {
300+ if (response == null ) {
301+ signInAnonymous (firebaseAuth );
302+ raiseEvent (callbackContext , "signinaborted" , null );
303+ return ;
304+ }
305+
306+ if (response .getError ().getErrorCode () == ErrorCodes .ANONYMOUS_UPGRADE_MERGE_CONFLICT ) {
307+ handleAnonymousUpgradeMergeConflict (response );
308+ } else {
309+
310+ if (response .getError ().getErrorCode () == ErrorCodes .NO_NETWORK ) {
311+ raiseErrorEvent ("signinfailure" , response .getError ().getErrorCode (), "No network so unable to sign in." );
312+
313+ return ;
314+ }
315+
316+ Log .e (TAG , "Sign-in error: " , response .getError ());
317+ raiseErrorEvent ("signinfailure" , response .getError ().getErrorCode (), "There was a problem signing in." );
318+ }
319+ }
320+ }
321+ }
322+
323+ private void handleAnonymousUpgradeMergeConflict (IdpResponse response ) {
324+ AuthCredential nonAnonymousCredential = response .getCredentialForLinking ();
325+
326+ FirebaseAuth .getInstance ().signInWithCredential (nonAnonymousCredential )
327+ .addOnSuccessListener (new OnSuccessListener <AuthResult >() {
328+ @ Override
329+ public void onSuccess (AuthResult result ) {
330+ FirebaseUser user = FirebaseAuth .getInstance ().getCurrentUser ();
331+ raiseEventForUser (user );
332+ }
333+ });
334+ }
335+
258336 private boolean signOut (CallbackContext callbackContext ) {
259337
260338 Log .d (TAG , "signOut" );
261339
262- AuthUI .getInstance ().signOut ((FragmentActivity ) cordova .getActivity ());
263- raiseEvent (callbackContext , "signoutsuccess" , null );
340+ AuthUI .getInstance ().signOut ((FragmentActivity ) cordova .getActivity ()).addOnCompleteListener (new OnCompleteListener <Void >() {
341+ @ Override
342+ public void onComplete (@ NonNull Task <Void > task ) {
343+ if (task .isSuccessful ()) {
264344
265- this .signInAnonymous (firebaseAuth );
345+ Log .d (TAG , "signOut: success" );
346+
347+ raiseEvent (callbackContext , "signoutsuccess" , null );
348+ signInAnonymous (firebaseAuth );
349+ }
350+ }
351+ }).addOnFailureListener (new OnFailureListener () {
352+ @ Override
353+ public void onFailure (@ NonNull Exception e ) {
354+ Log .w (TAG , "signOut: failure" , e );
355+ raiseErrorEvent ("signoutfailure" , 1 , "There was a problem signing out." );
356+ }
357+ });
266358
267359 return true ;
268360 }
@@ -365,25 +457,6 @@ private void raiseEventForUser(FirebaseUser user) {
365457
366458 }
367459
368- @ Override
369- public void onActivityResult (int requestCode , int resultCode , Intent data ) {
370- super .onActivityResult (requestCode , resultCode , data );
371-
372- Log .d (TAG , "onActivityResult" );
373- Log .d (TAG , "onActivityResult:requestCode:" + requestCode );
374-
375- if (requestCode == RC_SIGN_IN ) {
376- Log .d (TAG , "onActivityResult:resultCode:" + resultCode );
377-
378- if (resultCode == RESULT_OK ) {
379-
380- FirebaseUser user = FirebaseAuth .getInstance ().getCurrentUser ();
381- raiseEventForUser (user );
382- }
383- }
384- }
385-
386-
387460 private void raiseEvent (CallbackContext callbackContext , String type , Object data ) {
388461
389462 Log .d (TAG , "raiseEvent: " + type );
@@ -419,22 +492,4 @@ public void onComplete(@NonNull Task<AuthResult> task) {
419492 raiseEvent (callbackContext , "signinfailure" , data );
420493 }
421494 }
422-
423- @ Override
424- public void onAuthStateChanged (@ NonNull FirebaseAuth firebaseAuth ) {
425-
426- Log .d (TAG , "onAuthStateChanged" );
427-
428- final FirebaseUser user = firebaseAuth .getCurrentUser ();
429- if (user != null ) {
430- Log .d (TAG , "onAuthStateChanged: signed in" );
431-
432- raiseEventForUser (user );
433-
434- } else {
435- Log .d (TAG , "onAuthStateChanged: signed out" );
436-
437- raiseEvent (callbackContext , "signoutsuccess" , null );
438- }
439- }
440495}
0 commit comments