Skip to content

Commit d4565f9

Browse files
committed
fix(auth-emailpass): allow email update in updateProvider method
- Added email field to update method type signature - Email is now saved to user_metadata when passed to updateProvider - Password remains optional, both fields can be updated independently - Added integration tests for email and password update scenarios Fixes #14921
1 parent b49f6a2 commit d4565f9

File tree

2 files changed

+106
-27
lines changed

2 files changed

+106
-27
lines changed

packages/modules/providers/auth-emailpass/integration-tests/__tests__/services.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,81 @@ describe("Email password auth provider", () => {
271271
expect(authServiceSpies.retrieve).toHaveBeenCalled()
272272

273273
expect(resp.error).toEqual("Invalid email or password")
274+
275+
})
276+
277+
it("returns error if entity_id is not passed to update", async () => {
278+
const resp = await emailpassService.update(
279+
{ entity_id: "" },
280+
{} as any
281+
)
282+
283+
expect(resp).toEqual({
284+
success: false,
285+
error: "Cannot update emailpass provider identity without entity_id",
286+
})
287+
})
288+
289+
it("updates password if password is passed", async () => {
290+
const authServiceSpies = {
291+
update: jest.fn().mockImplementation(() => {
292+
return {
293+
provider_identities: [
294+
{
295+
entity_id: "test@admin.com",
296+
provider: "emailpass",
297+
provider_metadata: {},
298+
},
299+
],
300+
}
301+
}),
302+
}
303+
304+
const resp = await emailpassService.update(
305+
{ entity_id: "test@admin.com", password: "newpassword" },
306+
authServiceSpies as any
307+
)
308+
309+
expect(authServiceSpies.update).toHaveBeenCalledWith(
310+
"test@admin.com",
311+
expect.objectContaining({
312+
provider_metadata: expect.objectContaining({
313+
password: expect.any(String),
314+
}),
315+
})
316+
)
317+
expect(resp.success).toBe(true)
318+
})
319+
320+
it("updates email if email is passed", async () => {
321+
const authServiceSpies = {
322+
update: jest.fn().mockImplementation(() => {
323+
return {
324+
provider_identities: [
325+
{
326+
entity_id: "test@admin.com",
327+
provider: "emailpass",
328+
provider_metadata: {},
329+
},
330+
],
331+
}
332+
}),
333+
}
334+
335+
const resp = await emailpassService.update(
336+
{ entity_id: "test@admin.com", email: "newemail@admin.com" },
337+
authServiceSpies as any
338+
)
339+
340+
expect(authServiceSpies.update).toHaveBeenCalledWith(
341+
"test@admin.com",
342+
expect.objectContaining({
343+
user_metadata: expect.objectContaining({
344+
email: "newemail@admin.com",
345+
}),
346+
})
347+
)
348+
expect(resp.success).toBe(true)
274349
})
275350
})
351+

packages/modules/providers/auth-emailpass/src/services/emailpass.ts

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,44 @@ export class EmailPassAuthService extends AbstractAuthModuleProvider {
4646
}
4747

4848
async update(
49-
data: { password: string; entity_id: string },
50-
authIdentityService: AuthIdentityProviderService
51-
) {
52-
const { password, entity_id } = data ?? {}
49+
data: { password?: string; email?: string; entity_id: string },
50+
authIdentityService: AuthIdentityProviderService
51+
) {
52+
const { password, email, entity_id } = data ?? {}
5353

54-
if (!entity_id) {
55-
return {
56-
success: false,
57-
error: `Cannot update ${this.provider} provider identity without entity_id`,
58-
}
54+
if (!entity_id) {
55+
return {
56+
success: false,
57+
error: `Cannot update ${this.provider} provider identity without entity_id`,
5958
}
59+
}
6060

61-
if (!password || !isString(password)) {
62-
return { success: true }
63-
}
61+
let authIdentity
62+
const providerMetadataUpdate: Record<string, unknown> = {}
63+
const userMetadataUpdate: Record<string, unknown> = {}
6464

65-
let authIdentity
65+
if (password && isString(password)) {
66+
providerMetadataUpdate.password = await this.hashPassword(password)
67+
}
6668

67-
try {
68-
const passwordHash = await this.hashPassword(password)
69+
if (email && isString(email)) {
70+
userMetadataUpdate.email = email
71+
}
6972

70-
authIdentity = await authIdentityService.update(entity_id, {
71-
provider_metadata: {
72-
password: passwordHash,
73-
},
74-
})
75-
} catch (error) {
76-
return { success: false, error: error.message }
77-
}
73+
try {
74+
authIdentity = await authIdentityService.update(entity_id, {
75+
provider_metadata: providerMetadataUpdate,
76+
user_metadata: userMetadataUpdate,
77+
})
78+
} catch (error) {
79+
return { success: false, error: error.message }
80+
}
7881

79-
return {
80-
success: true,
81-
authIdentity,
82-
}
82+
return {
83+
success: true,
84+
authIdentity,
8385
}
86+
}
8487

8588
async authenticate(
8689
userData: AuthenticationInput,

0 commit comments

Comments
 (0)