@@ -4,7 +4,7 @@ import { ios as iOSUtils } from "tns-core-modules/utils/utils";
44import { getClass } from "tns-core-modules/utils/types" ;
55import { device } from "tns-core-modules/platform" ;
66import { DeviceType } from "tns-core-modules/ui/enums" ;
7- import { firestore } from "./firebase" ;
7+ import { firestore , User } from "./firebase" ;
88
99firebase . _messagingConnected = null ;
1010firebase . _pendingNotifications = [ ] ;
@@ -730,7 +730,7 @@ firebase.init = arg => {
730730 firebase . authStateListener = ( auth , user ) => {
731731 arg . onAuthStateChanged ( {
732732 loggedIn : user !== null ,
733- user : toLoginResult ( user )
733+ user : toLoginResult ( user , null )
734734 } ) ;
735735 } ;
736736 FIRAuth . auth ( ) . addAuthStateDidChangeListener ( firebase . authStateListener ) ;
@@ -741,7 +741,7 @@ firebase.init = arg => {
741741 firebase . authStateListener = ( auth , user ) => {
742742 firebase . notifyAuthStateListeners ( {
743743 loggedIn : user !== null ,
744- user : toLoginResult ( user )
744+ user : toLoginResult ( user , null )
745745 } ) ;
746746 } ;
747747 FIRAuth . auth ( ) . addAuthStateDidChangeListener ( firebase . authStateListener ) ;
@@ -1052,7 +1052,7 @@ firebase.getCurrentUser = arg => {
10521052
10531053 const user = fAuth . currentUser ;
10541054 if ( user ) {
1055- resolve ( toLoginResult ( user ) ) ;
1055+ resolve ( toLoginResult ( user , null ) ) ;
10561056 } else {
10571057 reject ( ) ;
10581058 }
@@ -1114,9 +1114,9 @@ firebase.logout = arg => {
11141114 } ) ;
11151115} ;
11161116
1117- function toLoginResult ( user ) {
1117+ function toLoginResult ( user , additionalUserInfo ) : User {
11181118 if ( ! user ) {
1119- return false ;
1119+ return null ;
11201120 }
11211121
11221122 const providers = [ ] ;
@@ -1134,19 +1134,34 @@ function toLoginResult(user) {
11341134 }
11351135 }
11361136
1137- return {
1138- uid : user . uid ,
1139- anonymous : user . anonymous ,
1140- isAnonymous : user . anonymous ,
1141- // provider: user.providerID, // always 'Firebase'
1142- providers : providers ,
1143- profileImageURL : user . photoURL ? user . photoURL . absoluteString : null ,
1144- email : user . email ,
1145- emailVerified : user . emailVerified ,
1146- name : user . displayName ,
1147- phoneNumber : user . phoneNumber ,
1148- refreshToken : user . refreshToken
1137+ const loginResult : User = {
1138+ uid : user . uid ,
1139+ anonymous : user . anonymous ,
1140+ isAnonymous : user . anonymous ,
1141+ // provider: user.providerID, // always 'Firebase'
1142+ providers : providers ,
1143+ profileImageURL : user . photoURL ? user . photoURL . absoluteString : null ,
1144+ email : user . email ,
1145+ emailVerified : user . emailVerified ,
1146+ name : user . displayName ,
1147+ phoneNumber : user . phoneNumber ,
1148+ refreshToken : user . refreshToken ,
1149+ metadata : {
1150+ creationTimestamp : user . metadata . creationDate as Date ,
1151+ lastSignInTimestamp : user . metadata . lastSignInDate as Date
1152+ }
11491153 } ;
1154+
1155+ if ( additionalUserInfo !== null ) {
1156+ loginResult . additionalUserInfo = {
1157+ profile : additionalUserInfo . profile ,
1158+ providerId : additionalUserInfo . providerID ,
1159+ username : additionalUserInfo . username ,
1160+ isNewUser : additionalUserInfo . newUser
1161+ } ;
1162+ }
1163+
1164+ return loginResult ;
11501165}
11511166
11521167firebase . getAuthToken = arg => {
@@ -1181,25 +1196,21 @@ firebase.getAuthToken = arg => {
11811196firebase . login = arg => {
11821197 return new Promise ( ( resolve , reject ) => {
11831198 try {
1184- const onCompletionWithUser = ( user : FIRUser , error ?: NSError ) => {
1185- if ( error ) {
1186- // also disconnect from Google otherwise ppl can't connect with a different account
1187- if ( typeof ( GIDSignIn ) !== "undefined" ) {
1188- GIDSignIn . sharedInstance ( ) . disconnect ( ) ;
1189- }
1190- reject ( error . localizedDescription ) ;
1191- } else {
1192- resolve ( toLoginResult ( user ) ) ;
1193-
1194- firebase . notifyAuthStateListeners ( {
1195- loggedIn : true ,
1196- user : user
1197- } ) ;
1198- }
1199- } ;
1200-
12011199 const onCompletionWithAuthResult = ( authResult : FIRAuthDataResult , error ?: NSError ) => {
1202- onCompletionWithUser ( authResult && authResult . user , error ) ;
1200+ if ( error ) {
1201+ // also disconnect from Google otherwise ppl can't connect with a different account
1202+ if ( typeof ( GIDSignIn ) !== "undefined" ) {
1203+ GIDSignIn . sharedInstance ( ) . disconnect ( ) ;
1204+ }
1205+ reject ( error . localizedDescription ) ;
1206+ } else {
1207+ resolve ( toLoginResult ( authResult && authResult . user , authResult && authResult . additionalUserInfo ) ) ;
1208+
1209+ firebase . notifyAuthStateListeners ( {
1210+ loggedIn : true ,
1211+ user : authResult . user
1212+ } ) ;
1213+ }
12031214 } ;
12041215
12051216 const fAuth = FIRAuth . auth ( ) ;
@@ -1222,16 +1233,16 @@ firebase.login = arg => {
12221233 const fIRAuthCredential = FIREmailAuthProvider . credentialWithEmailPassword ( arg . passwordOptions . email , arg . passwordOptions . password ) ;
12231234 if ( fAuth . currentUser ) {
12241235 // link credential, note that you only want to do this if this user doesn't already use fb as an auth provider
1225- const onCompletionLink = ( user : FIRUser , error : NSError ) => {
1236+ const onCompletionLink = ( authData : FIRAuthDataResult , error : NSError ) => {
12261237 if ( error ) {
12271238 // ignore, as this one was probably already linked, so just return the user
12281239 log ( "--- linking error: " + error . localizedDescription ) ;
1229- fAuth . signInWithCredentialCompletion ( fIRAuthCredential , onCompletionWithUser ) ;
1240+ fAuth . signInAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionWithAuthResult ) ;
12301241 } else {
1231- onCompletionWithUser ( user ) ;
1242+ onCompletionWithAuthResult ( authData , error ) ;
12321243 }
12331244 } ;
1234- fAuth . currentUser . linkWithCredentialCompletion ( fIRAuthCredential , onCompletionLink ) ;
1245+ fAuth . currentUser . linkAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionLink ) ;
12351246
12361247 } else {
12371248 fAuth . signInWithEmailPasswordCompletion ( arg . passwordOptions . email , arg . passwordOptions . password , onCompletionWithAuthResult ) ;
@@ -1289,17 +1300,17 @@ firebase.login = arg => {
12891300 firebase . requestPhoneAuthVerificationCode ( userResponse => {
12901301 const fIRAuthCredential = FIRPhoneAuthProvider . provider ( ) . credentialWithVerificationIDVerificationCode ( verificationID , userResponse ) ;
12911302 if ( fAuth . currentUser ) {
1292- const onCompletionLink = ( user , error ) => {
1303+ const onCompletionLink = ( authData : FIRAuthDataResult , error : NSError ) => {
12931304 if ( error ) {
12941305 // ignore, as this one was probably already linked, so just return the user
1295- fAuth . signInWithCredentialCompletion ( fIRAuthCredential , onCompletionWithUser ) ;
1306+ fAuth . signInAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionWithAuthResult ) ;
12961307 } else {
1297- onCompletionWithUser ( user ) ;
1308+ onCompletionWithAuthResult ( authData , error ) ;
12981309 }
12991310 } ;
1300- fAuth . currentUser . linkWithCredentialCompletion ( fIRAuthCredential , onCompletionLink ) ;
1311+ fAuth . currentUser . linkAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionLink ) ;
13011312 } else {
1302- fAuth . signInWithCredentialCompletion ( fIRAuthCredential , onCompletionWithUser ) ;
1313+ fAuth . signInAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionWithAuthResult ) ;
13031314 }
13041315 } , arg . phoneOptions . verificationPrompt ) ;
13051316 } ) ;
@@ -1311,12 +1322,12 @@ firebase.login = arg => {
13111322 }
13121323
13131324 if ( arg . customOptions . token ) {
1314- fAuth . signInWithCustomTokenCompletion ( arg . customOptions . token , onCompletionWithAuthResult ) ;
1325+ fAuth . signInAndRetrieveDataWithCustomTokenCompletion ( arg . customOptions . token , onCompletionWithAuthResult ) ;
13151326 } else if ( arg . customOptions . tokenProviderFn ) {
13161327 arg . customOptions . tokenProviderFn ( )
13171328 . then (
13181329 token => {
1319- fAuth . signInWithCustomTokenCompletion ( token , onCompletionWithAuthResult ) ;
1330+ fAuth . signInAndRetrieveDataWithCustomTokenCompletion ( token , onCompletionWithAuthResult ) ;
13201331 } ,
13211332 error => {
13221333 reject ( error ) ;
@@ -1342,19 +1353,19 @@ firebase.login = arg => {
13421353 const fIRAuthCredential = FIRFacebookAuthProvider . credentialWithAccessToken ( FBSDKAccessToken . currentAccessToken ( ) . tokenString ) ;
13431354 if ( fAuth . currentUser ) {
13441355 // link credential, note that you only want to do this if this user doesn't already use fb as an auth provider
1345- const onCompletionLink = ( user , error ) => {
1356+ const onCompletionLink = ( authData : FIRAuthDataResult , error : NSError ) => {
13461357 if ( error ) {
13471358 // ignore, as this one was probably already linked, so just return the user
13481359 log ( "--- linking error: " + error . localizedDescription ) ;
1349- fAuth . signInWithCredentialCompletion ( fIRAuthCredential , onCompletionWithUser ) ;
1360+ fAuth . signInAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionWithAuthResult ) ;
13501361 } else {
1351- onCompletionWithUser ( user ) ;
1362+ onCompletionWithAuthResult ( authData ) ;
13521363 }
13531364 } ;
1354- fAuth . currentUser . linkWithCredentialCompletion ( fIRAuthCredential , onCompletionLink ) ;
1365+ fAuth . currentUser . linkAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionLink ) ;
13551366
13561367 } else {
1357- fAuth . signInWithCredentialCompletion ( fIRAuthCredential , onCompletionWithUser ) ;
1368+ fAuth . signInAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionWithAuthResult ) ;
13581369 }
13591370 }
13601371 } ;
@@ -1400,15 +1411,15 @@ firebase.login = arg => {
14001411 const onCompletionLink = ( user , error ) => {
14011412 if ( error ) {
14021413 // ignore, as this one was probably already linked, so just return the user
1403- fAuth . signInWithCredentialCompletion ( fIRAuthCredential , onCompletionWithUser ) ;
1414+ fAuth . signInAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionWithAuthResult ) ;
14041415 } else {
1405- onCompletionWithUser ( user ) ;
1416+ onCompletionWithAuthResult ( user ) ;
14061417 }
14071418 } ;
1408- fAuth . currentUser . linkWithCredentialCompletion ( fIRAuthCredential , onCompletionLink ) ;
1419+ fAuth . currentUser . linkAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionLink ) ;
14091420
14101421 } else {
1411- fAuth . signInWithCredentialCompletion ( fIRAuthCredential , onCompletionWithUser ) ;
1422+ fAuth . signInAndRetrieveDataWithCredentialCompletion ( fIRAuthCredential , onCompletionWithAuthResult ) ;
14121423 }
14131424
14141425 } else {
0 commit comments