mirror of
https://github.com/logto-io/logto.git
synced 2025-01-13 21:30:30 -05:00
feat(core): provide management API to detect if user has password (#3698)
* feat(core): provide management API to detect if user has password * chore: add changeset * chore: add response status to koa guard Co-authored-by: Gao Sun <gao@silverhand.io> --------- Co-authored-by: Gao Sun <gao@silverhand.io>
This commit is contained in:
parent
5beeb5575b
commit
6b1948592a
3 changed files with 39 additions and 0 deletions
5
.changeset/popular-wasps-begin.md
Normal file
5
.changeset/popular-wasps-begin.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"@logto/core": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Provide management API to detect if a user has set the password.
|
|
@ -1,3 +1,4 @@
|
||||||
|
/* eslint-disable max-lines */
|
||||||
import type { CreateUser, Role, SignInExperience, User } from '@logto/schemas';
|
import type { CreateUser, Role, SignInExperience, User } from '@logto/schemas';
|
||||||
import { userInfoSelectFields } from '@logto/schemas';
|
import { userInfoSelectFields } from '@logto/schemas';
|
||||||
import { createMockUtils, pickDefault } from '@logto/shared/esm';
|
import { createMockUtils, pickDefault } from '@logto/shared/esm';
|
||||||
|
@ -375,6 +376,19 @@ describe('adminUserRoutes', () => {
|
||||||
expect(verifyUserPassword).toHaveBeenCalledWith(mockUser, password);
|
expect(verifyUserPassword).toHaveBeenCalledWith(mockUser, password);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('GET /users/:userId/has-password should return true if user has password', async () => {
|
||||||
|
const response = await userRequest.get(`/users/foo/has-password`);
|
||||||
|
expect(response.status).toEqual(200);
|
||||||
|
expect(response.body).toEqual({ hasPassword: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('GET /users/:userId/has-password should return false if user does not have password', async () => {
|
||||||
|
findUserById.mockImplementationOnce(async () => ({ ...mockUser, passwordEncrypted: null }));
|
||||||
|
const response = await userRequest.get(`/users/foo/has-password`);
|
||||||
|
expect(response.status).toEqual(200);
|
||||||
|
expect(response.body).toEqual({ hasPassword: false });
|
||||||
|
});
|
||||||
|
|
||||||
it('PATCH /users/:userId/is-suspended', async () => {
|
it('PATCH /users/:userId/is-suspended', async () => {
|
||||||
const mockedUserId = 'foo';
|
const mockedUserId = 'foo';
|
||||||
const response = await userRequest
|
const response = await userRequest
|
||||||
|
@ -460,3 +474,4 @@ describe('adminUserRoutes', () => {
|
||||||
expect(deleteUserIdentity).toHaveBeenCalledWith(arbitraryUserId, arbitraryTarget);
|
expect(deleteUserIdentity).toHaveBeenCalledWith(arbitraryUserId, arbitraryTarget);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
/* eslint-enable max-lines */
|
||||||
|
|
|
@ -255,6 +255,25 @@ export default function adminUserRoutes<T extends AuthedRouter>(
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
router.get(
|
||||||
|
'/users/:userId/has-password',
|
||||||
|
koaGuard({
|
||||||
|
params: object({ userId: string() }),
|
||||||
|
response: object({ hasPassword: boolean() }),
|
||||||
|
status: [200],
|
||||||
|
}),
|
||||||
|
async (ctx, next) => {
|
||||||
|
const { userId } = ctx.guard.params;
|
||||||
|
const user = await findUserById(userId);
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
hasPassword: Boolean(user.passwordEncrypted),
|
||||||
|
};
|
||||||
|
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
router.patch(
|
router.patch(
|
||||||
'/users/:userId/is-suspended',
|
'/users/:userId/is-suspended',
|
||||||
koaGuard({
|
koaGuard({
|
||||||
|
|
Loading…
Add table
Reference in a new issue