fix(auth-emailpass): better handle identity with same email error (#13537)

* fix(auth-emailpass): better handle identity with same email error

* add test

* Create blue-laws-argue.md

* check for empty object

* trueeee

* nit

* flip condition
This commit is contained in:
William Bouchard
2025-09-18 13:05:54 -04:00
committed by GitHub
parent a503bbe596
commit 0695c5844f
3 changed files with 126 additions and 27 deletions
@@ -1,6 +1,7 @@
import { MedusaError } from "@medusajs/framework/utils"
import Scrypt from "scrypt-kdf"
import { EmailPassAuthService } from "../../src/services/emailpass"
jest.setTimeout(100000)
describe("Email password auth provider", () => {
@@ -152,11 +153,83 @@ describe("Email password auth provider", () => {
)
})
it("throw if auth identity with email already exists", async () => {
it("updates identity if it exists but doesnt have app_metadata", async () => {
const authServiceSpies = {
retrieve: jest.fn().mockImplementation(() => {
return { success: true }
}),
update: jest.fn().mockImplementation(() => {
return {
provider_identities: [
{
entity_id: "test@admin.com",
provider: "emailpass",
provider_metadata: {
password: "somehash",
},
},
],
}
}),
}
const resp = await emailpassService.register(
{ body: { email: "test@admin.com", password: "test" } },
authServiceSpies
)
expect(authServiceSpies.retrieve).toHaveBeenCalled()
expect(authServiceSpies.update).toHaveBeenCalled()
expect(resp.authIdentity?.provider_identities?.[0]).toEqual(
expect.objectContaining({
entity_id: "test@admin.com",
provider_metadata: {},
})
)
})
it("updates identity if it exists but app_metadata is empty", async () => {
const authServiceSpies = {
retrieve: jest.fn().mockImplementation(() => {
return { success: true, app_metadata: {} }
}),
update: jest.fn().mockImplementation(() => {
return {
provider_identities: [
{
entity_id: "test@admin.com",
provider: "emailpass",
provider_metadata: {
password: "somehash",
},
},
],
}
}),
}
const resp = await emailpassService.register(
{ body: { email: "test@admin.com", password: "test" } },
authServiceSpies
)
expect(authServiceSpies.retrieve).toHaveBeenCalled()
expect(authServiceSpies.update).toHaveBeenCalled()
expect(resp.authIdentity?.provider_identities?.[0]).toEqual(
expect.objectContaining({
entity_id: "test@admin.com",
provider_metadata: {},
})
)
})
it("throw if auth identity with email already exists and has app_metadata", async () => {
const authServiceSpies = {
retrieve: jest.fn().mockImplementation(() => {
return { success: true, app_metadata: {"user_id": "some-id"} }
}),
create: jest.fn().mockImplementation(() => {
return {
provider_identities: [