0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

feat(core): delete user and update user password api (#248)

This commit is contained in:
Xiao Yijun 2022-02-18 17:02:08 +08:00 committed by GitHub
parent 9c3f017704
commit 9e30b41028
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,6 +10,7 @@ import koaPagination from '@/middleware/koa-pagination';
import { findRolesByRoleNames } from '@/queries/roles'; import { findRolesByRoleNames } from '@/queries/roles';
import { import {
clearUserCustomDataById, clearUserCustomDataById,
deleteUserById,
findAllUsers, findAllUsers,
findTotalNumberOfUsers, findTotalNumberOfUsers,
findUserById, findUserById,
@ -39,7 +40,7 @@ export default function adminUserRoutes<T extends AuthedRouter>(router: T) {
router.get( router.get(
'/users/:userId', '/users/:userId',
koaGuard({ koaGuard({
params: object({ userId: string().min(1) }), params: object({ userId: string() }),
}), }),
async (ctx, next) => { async (ctx, next) => {
const { const {
@ -97,7 +98,7 @@ export default function adminUserRoutes<T extends AuthedRouter>(router: T) {
router.patch( router.patch(
'/users/:userId', '/users/:userId',
koaGuard({ koaGuard({
params: object({ userId: string().min(1) }), params: object({ userId: string() }),
body: object({ body: object({
name: string().min(3).optional(), name: string().min(3).optional(),
avatar: string().url().optional(), avatar: string().url().optional(),
@ -122,10 +123,59 @@ export default function adminUserRoutes<T extends AuthedRouter>(router: T) {
} }
); );
router.patch(
'/users/:userId/password',
koaGuard({
params: object({ userId: string() }),
body: object({ password: string().min(6) }),
}),
async (ctx, next) => {
const {
params: { userId },
body: { password },
} = ctx.guard;
await findUserById(userId);
const { passwordEncryptionSalt, passwordEncrypted, passwordEncryptionMethod } =
encryptUserPassword(userId, password);
const user = await updateUserById(userId, {
passwordEncrypted,
passwordEncryptionMethod,
passwordEncryptionSalt,
});
ctx.body = pick(user, ...userInfoSelectFields);
return next();
}
);
router.delete(
'/users/:userId',
koaGuard({
params: object({ userId: string() }),
}),
async (ctx, next) => {
const {
params: { userId },
} = ctx.guard;
await findUserById(userId);
await deleteUserById(userId);
ctx.status = 204;
return next();
}
);
router.patch( router.patch(
'/users/:userId/roleNames', '/users/:userId/roleNames',
koaGuard({ koaGuard({
params: object({ userId: string().min(1) }), params: object({ userId: string() }),
body: object({ roleNames: string().array() }), body: object({ roleNames: string().array() }),
}), }),
async (ctx, next) => { async (ctx, next) => {
@ -159,7 +209,7 @@ export default function adminUserRoutes<T extends AuthedRouter>(router: T) {
router.patch( router.patch(
'/users/:userId/custom-data', '/users/:userId/custom-data',
koaGuard({ koaGuard({
params: object({ userId: string().min(1) }), params: object({ userId: string() }),
body: object({ customData: customDataGuard }), body: object({ customData: customDataGuard }),
}), }),
async (ctx, next) => { async (ctx, next) => {
@ -183,7 +233,7 @@ export default function adminUserRoutes<T extends AuthedRouter>(router: T) {
router.delete( router.delete(
'/users/:userId/custom-data', '/users/:userId/custom-data',
koaGuard({ koaGuard({
params: object({ userId: string().min(1) }), params: object({ userId: string() }),
}), }),
async (ctx, next) => { async (ctx, next) => {
const { const {