@@ -82,6 +82,8 @@ firebase.getCallbackData = function(type, snapshot) {
8282 } ;
8383} ;
8484
85+ firebase . authStateListener = null ;
86+
8587firebase . init = function ( arg ) {
8688 return new Promise ( function ( resolve , reject ) {
8789 try {
@@ -95,6 +97,21 @@ firebase.init = function (arg) {
9597 // it is however still possible to pass the URL programmatically (which we'll do for now):
9698 // instance = fDatabase.getInstance().getReferenceFromUrl(arg.url);
9799
100+ // Listen to auth state changes
101+ if ( ! firebase . authStateListener ) {
102+ firebase . authStateListener = new com . google . firebase . auth . FirebaseAuth . AuthStateListener ( {
103+ onAuthStateChanged : function ( fbAuth ) {
104+ var user = fbAuth . getCurrentUser ( ) ;
105+ firebase . notifyAuthStateListeners ( {
106+ loggedIn : user !== null ,
107+ user : toLoginResult ( user )
108+ } ) ;
109+ }
110+ } ) ;
111+ var firebaseAuth = com . google . firebase . auth . FirebaseAuth . getInstance ( ) ;
112+ firebaseAuth . addAuthStateListener ( firebase . authStateListener ) ;
113+ }
114+
98115 resolve ( instance ) ;
99116 } catch ( ex ) {
100117 console . log ( "Error in firebase.init: " + ex ) ;
@@ -115,42 +132,29 @@ firebase.logout = function (arg) {
115132 } ) ;
116133} ;
117134
118- firebase . authStateListener = null ;
135+ function toLoginResult ( user ) {
136+ return user && {
137+ uid : user . getUid ( ) ,
138+ name : user . getDisplayName ( ) ,
139+ email : user . getEmail ( ) ,
140+ // TODO add these properties, see https://firebase.google.com/docs/auth/android/manage-users#get_a_users_profile
141+ // provider: authData.getProvider(),
142+ // expiresAtUnixEpochSeconds: authData.getExpires(),
143+ profileImageURL : user . getPhotoUrl ( )
144+ } ;
145+ }
119146
120147firebase . login = function ( arg ) {
121148 return new Promise ( function ( resolve , reject ) {
122149 try {
123150 var firebaseAuth = com . google . firebase . auth . FirebaseAuth . getInstance ( ) ;
124-
125- if ( firebase . authStateListener === null ) {
126- firebase . authStateListener = new com . google . firebase . auth . FirebaseAuth . AuthStateListener ( {
127- onAuthStateChanged : function ( fbAuth ) {
128- var user = fbAuth . getCurrentUser ( ) ;
129- firebase . notifyAuthStateListeners ( {
130- loggedIn : user !== null ,
131- user : user
132- } ) ;
133- }
134- } ) ;
135- firebaseAuth . addAuthStateListener ( firebase . authStateListener ) ;
136- }
137-
138151 var onCompleteListener = new com . google . android . gms . tasks . OnCompleteListener ( {
139152 onComplete : function ( task ) {
140153 if ( ! task . isSuccessful ( ) ) {
141154 reject ( "Logging in the user failed" ) ;
142155 } else {
143156 var user = task . getResult ( ) . getUser ( ) ;
144- resolve ( {
145- uid : user . getUid ( ) ,
146- name : user . getDisplayName ( ) ,
147- email : user . getEmail ( ) ,
148- // TODO add these properties, see https://firebase.google.com/docs/auth/android/manage-users#get_a_users_profile
149- // provider: authData.getProvider(),
150- // expiresAtUnixEpochSeconds: authData.getExpires(),
151- profileImageURL : user . getPhotoUrl ( ) ,
152- //token: user.getToken() // can be used to auth with a backend server
153- } ) ;
157+ resolve ( toLoginResult ( user ) ) ;
154158 }
155159 }
156160 } ) ;
0 commit comments