Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 323e24c

Browse files
Merge pull request #764 from RaphaelJenni/master
Adding additionalUserInfo and metadata to sign in response #763
2 parents c3427fc + d7754ef commit 323e24c

File tree

3 files changed

+123
-76
lines changed

3 files changed

+123
-76
lines changed

src/firebase.android.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ad as AndroidUtils, layout } from "tns-core-modules/utils/utils";
55
import lazy from "tns-core-modules/utils/lazy";
66
import { topmost } from "tns-core-modules/ui/frame";
77
import { File } from "tns-core-modules/file-system";
8-
import { firestore } from "./firebase";
8+
import {firestore, User} from "./firebase";
99

1010
declare const android, com, org: any;
1111

@@ -316,7 +316,7 @@ firebase.init = arg => {
316316
const user = fbAuth.getCurrentUser();
317317
arg.onAuthStateChanged({
318318
loggedIn: user !== null,
319-
user: toLoginResult(user)
319+
user: toLoginResult(user, null)
320320
});
321321
}
322322
});
@@ -330,7 +330,7 @@ firebase.init = arg => {
330330
const user = fbAuth.getCurrentUser();
331331
firebase.notifyAuthStateListeners({
332332
loggedIn: user !== null,
333-
user: toLoginResult(user)
333+
user: toLoginResult(user, null)
334334
});
335335
}
336336
});
@@ -882,7 +882,7 @@ firebase.getCurrentUser = arg => {
882882
const firebaseAuth = com.google.firebase.auth.FirebaseAuth.getInstance();
883883
const user = firebaseAuth.getCurrentUser();
884884
if (user !== null) {
885-
resolve(toLoginResult(user));
885+
resolve(toLoginResult(user, null));
886886
} else {
887887
reject();
888888
}
@@ -974,9 +974,9 @@ firebase.getAuthToken = arg => {
974974
});
975975
};
976976

977-
function toLoginResult(user) {
977+
function toLoginResult(user, additionalUserInfo): User {
978978
if (user === null) {
979-
return false;
979+
return null;
980980
}
981981

982982
// for convenience return the result in multiple formats
@@ -992,18 +992,33 @@ function toLoginResult(user) {
992992
}
993993
}
994994

995-
return {
996-
uid: user.getUid(),
997-
name: user.getDisplayName(),
998-
email: user.getEmail(),
999-
emailVerified: user.isEmailVerified(),
1000-
// provider: user.getProviderId(), // always 'firebase'
1001-
providers: providers,
1002-
anonymous: user.isAnonymous(),
1003-
isAnonymous: user.isAnonymous(),
1004-
phoneNumber: user.getPhoneNumber(),
1005-
profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null
1006-
};
995+
const loginResult: User = {
996+
uid: user.getUid(),
997+
name: user.getDisplayName(),
998+
email: user.getEmail(),
999+
emailVerified: user.isEmailVerified(),
1000+
// provider: user.getProviderId(), // always 'firebase'
1001+
providers: providers,
1002+
anonymous: user.isAnonymous(),
1003+
isAnonymous: user.isAnonymous(),
1004+
phoneNumber: user.getPhoneNumber(),
1005+
profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null,
1006+
metadata: {
1007+
creationTimestamp: new Date(user.getMetadata().getCreationTimestamp() as number),
1008+
lastSignInTimestamp: new Date(user.getMetadata().getLastSignInTimestamp() as number)
1009+
}
1010+
};
1011+
1012+
if (additionalUserInfo !== null) {
1013+
loginResult.additionalUserInfo = {
1014+
profile: additionalUserInfo.getProfile(),
1015+
providerId: additionalUserInfo.getProviderId(),
1016+
username: additionalUserInfo.getUsername(),
1017+
isNewUser: additionalUserInfo.isNewUser()
1018+
};
1019+
}
1020+
1021+
return loginResult;
10071022
}
10081023

10091024
firebase.login = arg => {
@@ -1032,7 +1047,8 @@ firebase.login = arg => {
10321047
this.reject("Logging in the user failed. " + (task.getException() && task.getException().getReason ? task.getException().getReason() : task.getException()));
10331048
} else {
10341049
const user = task.getResult().getUser();
1035-
this.resolve(toLoginResult(user));
1050+
const additionalUserInfo = task.getResult().getAdditionalUserInfo();
1051+
this.resolve(toLoginResult(user, additionalUserInfo));
10361052
}
10371053
}
10381054
});
@@ -1106,7 +1122,7 @@ firebase.login = arg => {
11061122

11071123
if (user && firebase._alreadyLinkedToAuthProvider(user, "phone")) {
11081124
// skip sending an SMS if user is already linked to the phone-provider
1109-
resolve(toLoginResult(user));
1125+
resolve(toLoginResult(user, null));
11101126
return;
11111127
}
11121128

src/firebase.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,30 @@ export interface User {
299299
isAnonymous: boolean; // This is used by the web API
300300
providers: Array<Provider>;
301301
profileImageURL?: string;
302+
metadata: UserMetadata;
303+
additionalUserInfo?: AdditionalUserInfo
302304
/** iOS only */
303305
refreshToken?: string;
304306
}
305307

308+
/**
309+
* The metadata of the user
310+
*/
311+
export interface UserMetadata {
312+
creationTimestamp: Date,
313+
lastSignInTimestamp: Date
314+
}
315+
316+
/**
317+
* Contains additional user information
318+
*/
319+
export interface AdditionalUserInfo {
320+
profile: Map<string, any>;
321+
providerId: string;
322+
username: string;
323+
isNewUser: boolean
324+
}
325+
306326
/**
307327
* The returned object from the push function.
308328
*/

src/firebase.ios.ts

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ios as iOSUtils } from "tns-core-modules/utils/utils";
44
import { getClass } from "tns-core-modules/utils/types";
55
import { device } from "tns-core-modules/platform";
66
import { DeviceType } from "tns-core-modules/ui/enums";
7-
import { firestore } from "./firebase";
7+
import {firestore, User} from "./firebase";
88

99
firebase._messagingConnected = null;
1010
firebase._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

11521167
firebase.getAuthToken = arg => {
@@ -1181,25 +1196,21 @@ firebase.getAuthToken = arg => {
11811196
firebase.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

Comments
 (0)