mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -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) =>
|
||||
envSet.pool.one<User>(
|
||||
envSet.pool.maybeOne<User>(
|
||||
sql`
|
||||
select ${sql.join(Object.values(fields), sql`,`)}
|
||||
from ${table}
|
||||
|
|
|
@ -45,7 +45,7 @@ jest.mock('#src/lib/social.js', () => ({
|
|||
const insertUser = jest.fn(async (..._args: unknown[]) => mockUser);
|
||||
const findUserById = jest.fn(async (): Promise<User> => 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', () => ({
|
||||
findUserById: async () => findUserById(),
|
||||
|
@ -275,9 +275,9 @@ describe('session -> socialRoutes', () => {
|
|||
});
|
||||
|
||||
it('throw error when identity exists', async () => {
|
||||
const wrongConnectorTarget = 'wrongConnectorTarget';
|
||||
findUserByIdentity.mockResolvedValueOnce(null);
|
||||
(getLogtoConnectorById as jest.Mock).mockResolvedValueOnce({
|
||||
metadata: { target: wrongConnectorTarget },
|
||||
metadata: { target: connectorTarget },
|
||||
dbEntry: { syncProfile: false },
|
||||
});
|
||||
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);
|
||||
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(
|
||||
ctx,
|
||||
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;
|
||||
assertThat(!isSuspended, new RequestError({ code: 'user.suspended', status: 401 }));
|
||||
ctx.log(type, { userId: id });
|
||||
|
|
Loading…
Reference in a new issue