0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -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:
Charles Zhao 2023-04-14 12:22:33 +08:00 committed by GitHub
parent 5beeb5575b
commit 6b1948592a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
"@logto/core": patch
---
Provide management API to detect if a user has set the password.

View file

@ -1,3 +1,4 @@
/* eslint-disable max-lines */
import type { CreateUser, Role, SignInExperience, User } from '@logto/schemas';
import { userInfoSelectFields } from '@logto/schemas';
import { createMockUtils, pickDefault } from '@logto/shared/esm';
@ -375,6 +376,19 @@ describe('adminUserRoutes', () => {
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 () => {
const mockedUserId = 'foo';
const response = await userRequest
@ -460,3 +474,4 @@ describe('adminUserRoutes', () => {
expect(deleteUserIdentity).toHaveBeenCalledWith(arbitraryUserId, arbitraryTarget);
});
});
/* eslint-enable max-lines */

View file

@ -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(
'/users/:userId/is-suspended',
koaGuard({