From 6e4e5ffc0d7b7381182076a87fcb0c3443afd1a4 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Wed, 9 Nov 2022 16:36:11 +0800 Subject: [PATCH] fix(core): add check required profile to password routes (#2357) --- .../core/src/routes/session/password.test.ts | 20 +++++++++---------- packages/core/src/routes/session/password.ts | 5 +++-- packages/core/src/routes/session/utils.ts | 4 +++- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/core/src/routes/session/password.test.ts b/packages/core/src/routes/session/password.test.ts index 212cd7645..0eaf50aae 100644 --- a/packages/core/src/routes/session/password.test.ts +++ b/packages/core/src/routes/session/password.test.ts @@ -8,29 +8,29 @@ import { createRequester } from '@/utils/test-utils'; import passwordRoutes, { registerRoute, signInRoute } from './password'; -const insertUser = jest.fn(async (..._args: unknown[]) => ({ id: 'id' })); +const insertUser = jest.fn(async (..._args: unknown[]) => mockUser); const hasUser = jest.fn(async (username: string) => username === 'username1'); const findUserById = jest.fn(async (): Promise => mockUser); -const updateUserById = jest.fn(async (..._args: unknown[]) => ({ id: 'id' })); +const updateUserById = jest.fn(async (..._args: unknown[]) => mockUser); const hasActiveUsers = jest.fn(async () => true); const findDefaultSignInExperience = jest.fn(async () => mockSignInExperience); jest.mock('@/queries/user', () => ({ findUserById: async () => findUserById(), - findUserByIdentity: async () => ({ id: 'id', identities: {} }), - findUserByPhone: async () => ({ id: 'id' }), - findUserByEmail: async () => ({ id: 'id' }), + findUserByIdentity: async () => ({ id: mockUser.id, identities: {} }), + findUserByPhone: async () => mockUser, + findUserByEmail: async () => mockUser, updateUserById: async (...args: unknown[]) => updateUserById(...args), hasUser: async (username: string) => hasUser(username), hasUserWithIdentity: async (connectorId: string, userId: string) => - connectorId === 'connectorId' && userId === 'id', + connectorId === 'connectorId' && userId === mockUser.id, hasUserWithPhone: async (phone: string) => phone === '13000000000', hasUserWithEmail: async (email: string) => email === 'a@a.com', hasActiveUsers: async () => hasActiveUsers(), async findUserByUsername(username: string) { const roleNames = username === 'admin' ? [UserRole.Admin] : []; - return { id: 'id', username, roleNames }; + return { ...mockUser, username, roleNames }; }, })); @@ -112,7 +112,7 @@ describe('session -> password routes', () => { expect.anything(), expect.anything(), // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - expect.objectContaining({ login: { accountId: 'id', ts: expect.any(Number) } }), + expect.objectContaining({ login: { accountId: mockUser.id, ts: expect.any(Number) } }), expect.anything() ); }); @@ -129,7 +129,7 @@ describe('session -> password routes', () => { expect.anything(), expect.anything(), // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - expect.objectContaining({ login: { accountId: 'id', ts: expect.any(Number) } }), + expect.objectContaining({ login: { accountId: mockUser.id, ts: expect.any(Number) } }), expect.anything() ); }); @@ -146,7 +146,7 @@ describe('session -> password routes', () => { expect.anything(), expect.anything(), // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - expect.objectContaining({ login: { accountId: 'id', ts: expect.any(Number) } }), + expect.objectContaining({ login: { accountId: mockUser.id, ts: expect.any(Number) } }), expect.anything() ); }); diff --git a/packages/core/src/routes/session/password.ts b/packages/core/src/routes/session/password.ts index 498d14f24..37a4a05ff 100644 --- a/packages/core/src/routes/session/password.ts +++ b/packages/core/src/routes/session/password.ts @@ -19,7 +19,7 @@ import { import assertThat from '@/utils/assert-that'; import type { AnonymousRouter } from '../types'; -import { getRoutePrefix, signInWithPassword } from './utils'; +import { checkRequiredProfile, getRoutePrefix, signInWithPassword } from './utils'; export const registerRoute = getRoutePrefix('register', 'password'); export const signInRoute = getRoutePrefix('sign-in', 'password'); @@ -171,7 +171,7 @@ export default function passwordRoutes(router: T, pro const { passwordEncrypted, passwordEncryptionMethod } = await encryptUserPassword(password); - await insertUser({ + const user = await insertUser({ id, username, passwordEncrypted, @@ -179,6 +179,7 @@ export default function passwordRoutes(router: T, pro roleNames, lastSignInAt: Date.now(), }); + await checkRequiredProfile(ctx, provider, user, signInExperience); await assignInteractionResults(ctx, provider, { login: { accountId: id } }); return next(); diff --git a/packages/core/src/routes/session/utils.ts b/packages/core/src/routes/session/utils.ts index 0a0273f66..8ce16d05e 100644 --- a/packages/core/src/routes/session/utils.ts +++ b/packages/core/src/routes/session/utils.ts @@ -219,9 +219,11 @@ export const signInWithPassword = async ( ctx.log(logType, logPayload); const user = await findUser(); - const { id } = await verifyUserPassword(user, password); + const verifiedUser = await verifyUserPassword(user, password); + const { id } = verifiedUser; ctx.log(logType, { userId: id }); await updateUserById(id, { lastSignInAt: Date.now() }); + await checkRequiredProfile(ctx, provider, verifiedUser, signInExperience); await assignInteractionResults(ctx, provider, { login: { accountId: id } }, true); };