Skip to content

Commit

Permalink
feat(auth): implement User#unlink
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavohenke committed Jun 30, 2020
1 parent 27a05c0 commit edf0d6d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
18 changes: 18 additions & 0 deletions auth/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,21 @@ describe("#updateProfile()", () => {
});
});
});

describe("#unlink()", () => {
const providerId = firebase.auth.GoogleAuthProvider.PROVIDER_ID;

it("unlinks and updates providerData", async () => {
const user = new User({ uid: "1", providerData: [new UserInfo("1", providerId, {})] }, store);
await user.unlink(providerId);
expect(user.providerData).toHaveLength(0);
expect(store.update).toHaveBeenCalledWith(user.uid, {
providerData: user.providerData,
});
});

it("throws if provider isn't linked", () => {
const user = new User({ uid: "1" }, store);
return expect(user.unlink(providerId)).rejects.toThrowError("auth/no-such-provider");
});
});
13 changes: 11 additions & 2 deletions auth/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,17 @@ export class User implements firebase.User, UserSchema {
return { ...self };
}

unlink(providerId: string): Promise<any> {
throw new Error("Method not implemented.");
async unlink(providerId: string): Promise<User> {
const index = this.providerData.findIndex((info) => info.providerId === providerId);
if (index === -1) {
throw new Error("auth/no-such-provider");
}

this.providerData.splice(index, 1);
this.store.update(this.uid, {
providerData: this.providerData,
});
return this;
}

updateEmail(newEmail: string): Promise<void> {
Expand Down

0 comments on commit edf0d6d

Please sign in to comment.