0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-31 22:51:25 -05:00

feat(core): post /users ()

This commit is contained in:
Xiao Yijun 2022-02-16 16:34:32 +08:00 committed by GitHub
parent dd55c66644
commit 56c9f00440
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,10 +3,20 @@ import pick from 'lodash.pick';
import { InvalidInputError } from 'slonik';
import { object, string } from 'zod';
import RequestError from '@/errors/RequestError';
import { encryptUserPassword, generateUserId } from '@/lib/user';
import koaGuard from '@/middleware/koa-guard';
import koaPagination from '@/middleware/koa-pagination';
import { findRolesByRoleNames } from '@/queries/roles';
import { findAllUsers, findTotalNumberOfUsers, findUserById, updateUserById } from '@/queries/user';
import {
findAllUsers,
findTotalNumberOfUsers,
findUserById,
hasUser,
insertUser,
updateUserById,
} from '@/queries/user';
import assertThat from '@/utils/assert-that';
import { AuthedRouter } from './types';
@ -43,6 +53,46 @@ export default function adminUserRoutes<T extends AuthedRouter>(router: T) {
}
);
router.post(
'/users',
koaGuard({
body: object({
username: string().min(3),
password: string().min(6),
name: string().min(3),
}),
}),
async (ctx, next) => {
const { username, password, name } = ctx.guard.body;
assertThat(
!(await hasUser(username)),
new RequestError({
code: 'user.username_exists_register',
status: 422,
})
);
const id = await generateUserId();
const { passwordEncryptionSalt, passwordEncrypted, passwordEncryptionMethod } =
encryptUserPassword(id, password);
const user = await insertUser({
id,
username,
passwordEncrypted,
passwordEncryptionMethod,
passwordEncryptionSalt,
name,
});
ctx.body = pick(user, ...userInfoSelectFields);
return next();
}
);
router.patch(
'/users/:userId/roleNames',
koaGuard({