@@ -116,7 +116,8 @@ firebase.authStateListener = null;
116116
117117firebase . init = function ( arg ) {
118118 return new Promise ( function ( resolve , reject ) {
119- try {
119+
120+ function _resolve ( ) {
120121 if ( firebase . instance !== null ) {
121122 reject ( "You already ran init" ) ;
122123 return ;
@@ -191,6 +192,15 @@ firebase.init = function (arg) {
191192 }
192193
193194 resolve ( firebase . instance ) ;
195+ }
196+
197+ try {
198+ if ( appModule . android . foregroundActivity ) {
199+ _resolve ( ) ;
200+ } else {
201+ // if this is called before application.start() wait for the event to fire
202+ appModule . on ( appModule . launchEvent , _resolve ) ;
203+ }
194204 } catch ( ex ) {
195205 console . log ( "Error in firebase.init: " + ex ) ;
196206 reject ( ex ) ;
@@ -420,65 +430,64 @@ function toLoginResult(user) {
420430
421431firebase . login = function ( arg ) {
422432 return new Promise ( function ( resolve , reject ) {
433+ try {
434+ if ( ! firebase . _isGooglePlayServicesAvailable ( ) ) {
435+ reject ( "Google Play services is required for this feature, but not available on this device" ) ;
436+ return ;
437+ }
423438
424- function _resolve ( ) {
425- if ( ! firebase . _isGooglePlayServicesAvailable ( ) ) {
426- reject ( "Google Play services is required for this feature, but not available on this device" ) ;
427- return ;
428- }
429-
430- var firebaseAuth = com . google . firebase . auth . FirebaseAuth . getInstance ( ) ;
431- var onCompleteListener = new com . google . android . gms . tasks . OnCompleteListener ( {
432- onComplete : function ( task ) {
433- if ( firebase . _rememberedContext !== null ) {
434- appModule . android . currentContext = firebase . _rememberedContext ;
435- firebase . _rememberedContext = null ;
436- }
437- if ( ! task . isSuccessful ( ) ) {
438- console . log ( "Logging in the user failed. " + ( task . getException ( ) && task . getException ( ) . getReason ? task . getException ( ) . getReason ( ) : task . getException ( ) ) ) ;
439- reject ( "Logging in the user failed. " + ( task . getException ( ) && task . getException ( ) . getReason ? task . getException ( ) . getReason ( ) : task . getException ( ) ) ) ;
440- } else {
441- var user = task . getResult ( ) . getUser ( ) ;
442- resolve ( toLoginResult ( user ) ) ;
443- }
439+ var firebaseAuth = com . google . firebase . auth . FirebaseAuth . getInstance ( ) ;
440+ var onCompleteListener = new com . google . android . gms . tasks . OnCompleteListener ( {
441+ onComplete : function ( task ) {
442+ if ( firebase . _rememberedContext !== null ) {
443+ appModule . android . currentContext = firebase . _rememberedContext ;
444+ firebase . _rememberedContext = null ;
444445 }
445- } ) ;
446-
447- if ( arg . type === firebase . LoginType . ANONYMOUS ) {
448- firebaseAuth . signInAnonymously ( ) . addOnCompleteListener ( onCompleteListener ) ;
449-
450- } else if ( arg . type === firebase . LoginType . PASSWORD ) {
451- if ( ! arg . email || ! arg . password ) {
452- reject ( "Auth type emailandpassword requires an email and password argument" ) ;
446+ if ( ! task . isSuccessful ( ) ) {
447+ console . log ( "Logging in the user failed. " + ( task . getException ( ) && task . getException ( ) . getReason ? task . getException ( ) . getReason ( ) : task . getException ( ) ) ) ;
448+ reject ( "Logging in the user failed. " + ( task . getException ( ) && task . getException ( ) . getReason ? task . getException ( ) . getReason ( ) : task . getException ( ) ) ) ;
453449 } else {
454- firebaseAuth . signInWithEmailAndPassword ( arg . email , arg . password ) . addOnCompleteListener ( onCompleteListener ) ;
450+ var user = task . getResult ( ) . getUser ( ) ;
451+ resolve ( toLoginResult ( user ) ) ;
455452 }
453+ }
454+ } ) ;
456455
457- } else if ( arg . type === firebase . LoginType . CUSTOM ) {
458- if ( ! arg . token && ! arg . tokenProviderFn ) {
459- reject ( "Auth type custom requires a token or a tokenProviderFn argument" ) ;
460- } else if ( arg . token ) {
461- firebaseAuth . signInWithCustomToken ( arg . token ) . addOnCompleteListener ( onCompleteListener ) ;
462- } else if ( arg . tokenProviderFn ) {
463- arg . tokenProviderFn ( )
464- . then (
465- function ( token ) {
466- firebaseAuth . signInWithCustomToken ( arg . token ) . addOnCompleteListener ( onCompleteListener ) ;
467- } ,
468- function ( error ) {
469- reject ( error ) ;
470- }
471- ) ;
472- }
456+ if ( arg . type === firebase . LoginType . ANONYMOUS ) {
457+ firebaseAuth . signInAnonymously ( ) . addOnCompleteListener ( onCompleteListener ) ;
473458
474- } else if ( arg . type === firebase . LoginType . FACEBOOK ) {
475- if ( typeof ( com . facebook ) === "undefined" ) {
476- reject ( "Facebook SDK not installed - see gradle config" ) ;
477- return ;
478- }
459+ } else if ( arg . type === firebase . LoginType . PASSWORD ) {
460+ if ( ! arg . email || ! arg . password ) {
461+ reject ( "Auth type emailandpassword requires an email and password argument" ) ;
462+ } else {
463+ firebaseAuth . signInWithEmailAndPassword ( arg . email , arg . password ) . addOnCompleteListener ( onCompleteListener ) ;
464+ }
479465
480- var fbLoginManager = com . facebook . login . LoginManager . getInstance ( ) ;
481- fbLoginManager . registerCallback (
466+ } else if ( arg . type === firebase . LoginType . CUSTOM ) {
467+ if ( ! arg . token && ! arg . tokenProviderFn ) {
468+ reject ( "Auth type custom requires a token or a tokenProviderFn argument" ) ;
469+ } else if ( arg . token ) {
470+ firebaseAuth . signInWithCustomToken ( arg . token ) . addOnCompleteListener ( onCompleteListener ) ;
471+ } else if ( arg . tokenProviderFn ) {
472+ arg . tokenProviderFn ( )
473+ . then (
474+ function ( token ) {
475+ firebaseAuth . signInWithCustomToken ( arg . token ) . addOnCompleteListener ( onCompleteListener ) ;
476+ } ,
477+ function ( error ) {
478+ reject ( error ) ;
479+ }
480+ ) ;
481+ }
482+
483+ } else if ( arg . type === firebase . LoginType . FACEBOOK ) {
484+ if ( typeof ( com . facebook ) === "undefined" ) {
485+ reject ( "Facebook SDK not installed - see gradle config" ) ;
486+ return ;
487+ }
488+
489+ var fbLoginManager = com . facebook . login . LoginManager . getInstance ( ) ;
490+ fbLoginManager . registerCallback (
482491 fbCallbackManager ,
483492 new com . facebook . FacebookCallback ( {
484493 onSuccess : function ( loginResult ) {
@@ -503,97 +512,87 @@ firebase.login = function (arg) {
503512 reject ( "Error while trying to login with Fb " + ex ) ;
504513 }
505514 } )
506- ) ;
515+ ) ;
507516
508- var scope = [ "public_profile" , "email" ] ;
509- if ( arg . scope ) {
510- scope = arg . scope ;
511- }
512- var permissions = utils . ad . collections . stringArrayToStringSet ( scope ) ;
517+ var scope = [ "public_profile" , "email" ] ;
518+ if ( arg . scope ) {
519+ scope = arg . scope ;
520+ }
521+ var permissions = utils . ad . collections . stringArrayToStringSet ( scope ) ;
513522
514- var activity = appModule . android . foregroundActivity ;
515- firebase . _rememberedContext = appModule . android . currentContext ;
516- fbLoginManager . logInWithReadPermissions ( activity , permissions ) ;
523+ var activity = appModule . android . foregroundActivity ;
524+ firebase . _rememberedContext = appModule . android . currentContext ;
525+ fbLoginManager . logInWithReadPermissions ( activity , permissions ) ;
517526
518- } else if ( arg . type === firebase . LoginType . GOOGLE ) {
519- if ( typeof ( com . google . android . gms . auth . api . Auth ) === "undefined" ) {
520- reject ( "Google Sign In not installed - see gradle config" ) ;
521- return ;
522- }
523-
524- var clientStringId = utils . ad . resources . getStringId ( "default_web_client_id" ) ;
525- var clientId = utils . ad . getApplicationContext ( ) . getResources ( ) . getString ( clientStringId ) ;
527+ } else if ( arg . type === firebase . LoginType . GOOGLE ) {
528+ if ( typeof ( com . google . android . gms . auth . api . Auth ) === "undefined" ) {
529+ reject ( "Google Sign In not installed - see gradle config" ) ;
530+ return ;
531+ }
526532
527- // Configure Google Sign In
528- var googleSignInOptions = new com . google . android . gms . auth . api . signin . GoogleSignInOptions . Builder ( com . google . android . gms . auth . api . signin . GoogleSignInOptions . DEFAULT_SIGN_IN )
529- . requestIdToken ( clientId )
530- . requestEmail ( )
531- . build ( ) ;
533+ var clientStringId = utils . ad . resources . getStringId ( "default_web_client_id" ) ;
534+ var clientId = utils . ad . getApplicationContext ( ) . getResources ( ) . getString ( clientStringId ) ;
532535
533- var onConnectionFailedListener = new com . google . android . gms . common . api . GoogleApiClient . OnConnectionFailedListener ( {
534- onConnectionFailed : function ( connectionResult ) {
535- reject ( connectionResult . getErrorMessage ( ) ) ;
536- }
537- } ) ;
536+ // Configure Google Sign In
537+ var googleSignInOptions = new com . google . android . gms . auth . api . signin . GoogleSignInOptions . Builder ( com . google . android . gms . auth . api . signin . GoogleSignInOptions . DEFAULT_SIGN_IN )
538+ . requestIdToken ( clientId )
539+ . requestEmail ( )
540+ . build ( ) ;
538541
539- var mGoogleApiClient = new com . google . android . gms . common . api . GoogleApiClient . Builder ( appModule . android . context )
540- . addOnConnectionFailedListener ( onConnectionFailedListener )
541- . addApi ( com . google . android . gms . auth . api . Auth . GOOGLE_SIGN_IN_API , googleSignInOptions )
542- . build ( ) ;
542+ var onConnectionFailedListener = new com . google . android . gms . common . api . GoogleApiClient . OnConnectionFailedListener ( {
543+ onConnectionFailed : function ( connectionResult ) {
544+ reject ( connectionResult . getErrorMessage ( ) ) ;
545+ }
546+ } ) ;
543547
544- var signInIntent = com . google . android . gms . auth . api . Auth . GoogleSignInApi . getSignInIntent ( mGoogleApiClient ) ;
548+ var mGoogleApiClient = new com . google . android . gms . common . api . GoogleApiClient . Builder ( appModule . android . context )
549+ . addOnConnectionFailedListener ( onConnectionFailedListener )
550+ . addApi ( com . google . android . gms . auth . api . Auth . GOOGLE_SIGN_IN_API , googleSignInOptions )
551+ . build ( ) ;
545552
546- firebase . _rememberedContext = appModule . android . currentContext ;
547- appModule . android . currentContext . startActivityForResult ( signInIntent , GOOGLE_SIGNIN_INTENT_ID ) ;
553+ var signInIntent = com . google . android . gms . auth . api . Auth . GoogleSignInApi . getSignInIntent ( mGoogleApiClient ) ;
548554
549- appModule . android . on ( appModule . AndroidApplication . activityResultEvent , function ( eventData ) {
550- if ( eventData . requestCode === GOOGLE_SIGNIN_INTENT_ID ) {
551- if ( firebase . _rememberedContext !== null ) {
552- appModule . android . currentContext = firebase . _rememberedContext ;
553- firebase . _rememberedContext = null ;
554- }
555- var googleSignInResult = com . google . android . gms . auth . api . Auth . GoogleSignInApi . getSignInResultFromIntent ( eventData . intent ) ;
556- var success = googleSignInResult . isSuccess ( ) ;
557- if ( success ) {
558- var googleSignInAccount = googleSignInResult . getSignInAccount ( ) ;
559- var idToken = googleSignInAccount . getIdToken ( ) ;
560- var accessToken = null ;
561- var authCredential = com . google . firebase . auth . GoogleAuthProvider . getCredential ( idToken , accessToken ) ;
555+ firebase . _rememberedContext = appModule . android . currentContext ;
556+ appModule . android . currentContext . startActivityForResult ( signInIntent , GOOGLE_SIGNIN_INTENT_ID ) ;
562557
563- var user = com . google . firebase . auth . FirebaseAuth . getInstance ( ) . getCurrentUser ( ) ;
564- if ( user ) {
565- if ( firebase . _alreadyLinkedToAuthProvider ( user , "google.com" ) ) {
566- firebaseAuth . signInWithCredential ( authCredential ) . addOnCompleteListener ( onCompleteListener ) ;
567- } else {
568- user . linkWithCredential ( authCredential ) . addOnCompleteListener ( onCompleteListener ) ;
569- }
570- } else {
558+ appModule . android . on ( appModule . AndroidApplication . activityResultEvent , function ( eventData ) {
559+ if ( eventData . requestCode === GOOGLE_SIGNIN_INTENT_ID ) {
560+ if ( firebase . _rememberedContext !== null ) {
561+ appModule . android . currentContext = firebase . _rememberedContext ;
562+ firebase . _rememberedContext = null ;
563+ }
564+ var googleSignInResult = com . google . android . gms . auth . api . Auth . GoogleSignInApi . getSignInResultFromIntent ( eventData . intent ) ;
565+ var success = googleSignInResult . isSuccess ( ) ;
566+ if ( success ) {
567+ var googleSignInAccount = googleSignInResult . getSignInAccount ( ) ;
568+ var idToken = googleSignInAccount . getIdToken ( ) ;
569+ var accessToken = null ;
570+ var authCredential = com . google . firebase . auth . GoogleAuthProvider . getCredential ( idToken , accessToken ) ;
571+
572+ var user = com . google . firebase . auth . FirebaseAuth . getInstance ( ) . getCurrentUser ( ) ;
573+ if ( user ) {
574+ if ( firebase . _alreadyLinkedToAuthProvider ( user , "google.com" ) ) {
571575 firebaseAuth . signInWithCredential ( authCredential ) . addOnCompleteListener ( onCompleteListener ) ;
576+ } else {
577+ user . linkWithCredential ( authCredential ) . addOnCompleteListener ( onCompleteListener ) ;
572578 }
573579 } else {
574- console . log ( "Make sure you've uploaded you SHA1 fingerprint(s) to the Firebase console" ) ;
575- reject ( "Has the SHA1 fingerprint been uploaded? Sign-in status: " + googleSignInResult . getStatus ( ) ) ;
580+ firebaseAuth . signInWithCredential ( authCredential ) . addOnCompleteListener ( onCompleteListener ) ;
576581 }
582+ } else {
583+ console . log ( "Make sure you've uploaded you SHA1 fingerprint(s) to the Firebase console" ) ;
584+ reject ( "Has the SHA1 fingerprint been uploaded? Sign-in status: " + googleSignInResult . getStatus ( ) ) ;
577585 }
578- } ) ;
579-
580- } else {
581- reject ( "Unsupported auth type: " + arg . type ) ;
582- }
583- }
584-
586+ }
587+ } ) ;
585588
586- try {
587- if ( appModule . android . foregroundActivity ) {
588- _resolve ( ) ;
589- } else {
590- // if this is called before application.start() wait for the event to fire
591- appModule . on ( appModule . launchEvent , _resolve ) ;
592- }
593- } catch ( ex ) {
594- console . log ( "Error in firebase.login: " + ex ) ;
595- reject ( ex ) ;
589+ } else {
590+ reject ( "Unsupported auth type: " + arg . type ) ;
596591 }
592+ } catch ( ex ) {
593+ console . log ( "Error in firebase.login: " + ex ) ;
594+ reject ( ex ) ;
595+ }
597596 } ) ;
598597} ;
599598
0 commit comments