Skip to content

Commit

Permalink
refactor(auth): add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavohenke committed Jul 26, 2020
1 parent 97d39ec commit e62435b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ export class MockAuth implements firebase.auth.Auth {
}

private async signInWithSocial(provider: firebase.auth.AuthProvider) {
const data = await this.store.consumeSocialMock(provider);
let user = this.store.findByProviderAndEmail(data.email, provider.providerId);
const mockResponse = await this.store.consumeSocialMock(provider);
let user = this.store.findByProviderAndEmail(mockResponse.email, provider.providerId);
if (user) {
return this.signIn(user, { isNewUser: false });
}

user = this.store.add({ ...data, providerId: provider.providerId });
// user didn't exist, so it's created and then signed in
user = this.store.add({ ...mockResponse, providerId: provider.providerId });
return this.signIn(user, { isNewUser: true });
}

Expand Down
11 changes: 11 additions & 0 deletions auth/user-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ export class UserStore {
private store = new Map<string, UserSchema>();
private readonly socialMocks: SocialSignInMock[] = [];

/**
* Adds a user to this store with the given data.
* The new user is assigned a new ID and is returned.
*/
add(data: Partial<UserSchema>): User {
const uid = ++this.nextId + "";
const user = new User(
{
...data,
// If the user is being created with a provider ID, then its data is coming from that provider.
providerData: data.providerId ? [new UserInfo(uid, data.providerId, data)] : [],
uid,
},
Expand All @@ -26,6 +31,12 @@ export class UserStore {
this.socialMocks.push(mock);
}

/**
* Finds the mock response for a given AuthProvider, removes it from the list of mocks
* and returns its response.
*
* If a mock for that AuthProvider is not set, then this method throws.
*/
consumeSocialMock(provider: firebase.auth.AuthProvider) {
const index = this.socialMocks.findIndex((mock) => mock.type === provider.providerId);
if (index === -1) {
Expand Down

0 comments on commit e62435b

Please sign in to comment.