mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
refactor(core): refactor findUserByIdentity (#2537)
This commit is contained in:
parent
e2c739aa11
commit
5385a2cdd7
3 changed files with 8 additions and 6 deletions
|
@ -39,7 +39,7 @@ export const findUserById = async (id: string) =>
|
||||||
`);
|
`);
|
||||||
|
|
||||||
export const findUserByIdentity = async (target: string, userId: string) =>
|
export const findUserByIdentity = async (target: string, userId: string) =>
|
||||||
envSet.pool.one<User>(
|
envSet.pool.maybeOne<User>(
|
||||||
sql`
|
sql`
|
||||||
select ${sql.join(Object.values(fields), sql`,`)}
|
select ${sql.join(Object.values(fields), sql`,`)}
|
||||||
from ${table}
|
from ${table}
|
||||||
|
|
|
@ -45,7 +45,7 @@ jest.mock('#src/lib/social.js', () => ({
|
||||||
const insertUser = jest.fn(async (..._args: unknown[]) => mockUser);
|
const insertUser = jest.fn(async (..._args: unknown[]) => mockUser);
|
||||||
const findUserById = jest.fn(async (): Promise<User> => mockUser);
|
const findUserById = jest.fn(async (): Promise<User> => mockUser);
|
||||||
const updateUserById = jest.fn(async (..._args: unknown[]) => mockUser);
|
const updateUserById = jest.fn(async (..._args: unknown[]) => mockUser);
|
||||||
const findUserByIdentity = jest.fn(async () => mockUser);
|
const findUserByIdentity = jest.fn().mockResolvedValue(mockUser);
|
||||||
|
|
||||||
jest.mock('#src/queries/user.js', () => ({
|
jest.mock('#src/queries/user.js', () => ({
|
||||||
findUserById: async () => findUserById(),
|
findUserById: async () => findUserById(),
|
||||||
|
@ -275,9 +275,9 @@ describe('session -> socialRoutes', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throw error when identity exists', async () => {
|
it('throw error when identity exists', async () => {
|
||||||
const wrongConnectorTarget = 'wrongConnectorTarget';
|
findUserByIdentity.mockResolvedValueOnce(null);
|
||||||
(getLogtoConnectorById as jest.Mock).mockResolvedValueOnce({
|
(getLogtoConnectorById as jest.Mock).mockResolvedValueOnce({
|
||||||
metadata: { target: wrongConnectorTarget },
|
metadata: { target: connectorTarget },
|
||||||
dbEntry: { syncProfile: false },
|
dbEntry: { syncProfile: false },
|
||||||
});
|
});
|
||||||
const response = await sessionRequest.post(`${signInRoute}/auth`).send({
|
const response = await sessionRequest.post(`${signInRoute}/auth`).send({
|
||||||
|
|
|
@ -77,7 +77,10 @@ export default function socialRoutes<T extends AnonymousRouter>(router: T, provi
|
||||||
const userInfo = await getUserInfoByAuthCode(connectorId, data);
|
const userInfo = await getUserInfoByAuthCode(connectorId, data);
|
||||||
ctx.log(type, { userInfo });
|
ctx.log(type, { userInfo });
|
||||||
|
|
||||||
if (!(await hasUserWithIdentity(target, userInfo.id))) {
|
const user = await findUserByIdentity(target, userInfo.id);
|
||||||
|
|
||||||
|
// User with identity not found
|
||||||
|
if (!user) {
|
||||||
await assignInteractionResults(
|
await assignInteractionResults(
|
||||||
ctx,
|
ctx,
|
||||||
provider,
|
provider,
|
||||||
|
@ -95,7 +98,6 @@ export default function socialRoutes<T extends AnonymousRouter>(router: T, provi
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await findUserByIdentity(target, userInfo.id);
|
|
||||||
const { id, identities, isSuspended } = user;
|
const { id, identities, isSuspended } = user;
|
||||||
assertThat(!isSuspended, new RequestError({ code: 'user.suspended', status: 401 }));
|
assertThat(!isSuspended, new RequestError({ code: 'user.suspended', status: 401 }));
|
||||||
ctx.log(type, { userId: id });
|
ctx.log(type, { userId: id });
|
||||||
|
|
Loading…
Add table
Reference in a new issue