Skip to content

Commit

Permalink
feat(auth): implement additionalUserInfo
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
gustavohenke committed Jun 30, 2020
1 parent 070ff78 commit 0ae4267
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 9 additions & 2 deletions auth/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ describe("#signInWithPopup()", () => {
const auth = new MockAuth(app);
auth.mockSocialSignIn(provider).respondWithUser("Foo", "foo@bar.com");

const { user } = await auth.signInWithPopup(provider);
const { user, additionalUserInfo } = await auth.signInWithPopup(provider);
expect(auth.currentUser).toBe(user);
expect(additionalUserInfo).toEqual({
isNewUser: true,
providerId: provider.providerId,
profile: null,
username: "foo@bar.com",
});
expect(user).toHaveProperty("displayName", "Foo");
expect(user).toHaveProperty("email", "foo@bar.com");
expect(user).toHaveProperty("providerId", provider.providerId);
Expand All @@ -145,7 +151,8 @@ describe("#signInWithPopup()", () => {

const credential1 = await auth.signInWithPopup(provider);
const credential2 = await auth.signInWithPopup(provider);
expect(credential1).toEqual(credential2);
expect(credential2.user).toEqual(credential1.user);
expect(credential2.additionalUserInfo).toHaveProperty("isNewUser", false);
expect(auth.currentUser).toBe(credential2.user);
});

Expand Down
21 changes: 17 additions & 4 deletions auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,18 @@ export class MockAuth implements firebase.auth.Auth {
return Promise.resolve();
}

private signIn(user: User): Promise<firebase.auth.UserCredential> {
private signIn(
user: User,
additionalUserInfo: firebase.auth.AdditionalUserInfo | null = null
): Promise<firebase.auth.UserCredential> {
this.currentUser = user;
this.authStateEvents.forEach((listener) => {
listener(user);
});

return Promise.resolve<firebase.auth.UserCredential>({
user,
additionalUserInfo: null,
additionalUserInfo,
credential: null,
operationType: "signIn",
});
Expand Down Expand Up @@ -187,11 +190,21 @@ export class MockAuth implements firebase.auth.Auth {
throw new Error("auth/account-exists-with-different-credential");
}

return this.signIn(user);
return this.signIn(user, {
isNewUser: false,
providerId: provider.providerId,
profile: null,
username: data.email,
});
}

user = this.store.add({ ...data, providerId: provider.providerId });
return this.signIn(user);
return this.signIn(user, {
isNewUser: true,
providerId: provider.providerId,
profile: null,
username: data.email,
});
}

signInWithRedirect(provider: firebase.auth.AuthProvider): Promise<void> {
Expand Down

0 comments on commit 0ae4267

Please sign in to comment.