Skip to content

Commit

Permalink
Throw error message if user has no email from firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDimitras committed Oct 1, 2024
1 parent b2e854a commit de5558f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/commands/__tests__/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jest.mock("../../drivers/auth", () => ({
jest.mock("../../drivers/stdio");
jest.mock("../../plugins/login");

const mockSignInWithCredential = signInWithCredential as jest.Mock;
const mockReadFile = readFile as jest.Mock;
const mockWriteFile = writeFile as jest.Mock;

Expand Down Expand Up @@ -51,6 +52,14 @@ describe("login", () => {
mockWriteFile.mockImplementation(async (_path, data) => {
credentialData = data;
});
mockSignInWithCredential.mockImplementation(
async (_auth, _firebaseCredential) =>
Promise.resolve({
user: {
email: "user@p0.dev",
},
})
);
beforeEach(() => {
credentialData = "";
jest.clearAllMocks();
Expand All @@ -72,5 +81,14 @@ describe("login", () => {
await login({ org: "test-org" });
expect((signInWithCredential as jest.Mock).mock.calls).toMatchSnapshot();
});
it("returns an error message if firebase cannot determine the user's email", async () => {
mockSignInWithCredential.mockResolvedValueOnce({
user: {},
});
await expect(login({ org: "test-org" })).rejects.toMatchInlineSnapshot(`
"Can not sign in: this user has previously signed in with a different identity provider.
Please contact support@p0.dev to enable this user."
`);
});
});
});
3 changes: 3 additions & 0 deletions src/drivers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export const authenticate = async (options?: {
});
auth.tenantId = identity.org.tenantId;
const userCredential = await signInWithCredential(auth, firebaseCredential);
if (!userCredential?.user?.email) {
throw "Can not sign in: this user has previously signed in with a different identity provider.\nPlease contact support@p0.dev to enable this user.";
}

return { userCredential, identity };
};

0 comments on commit de5558f

Please sign in to comment.