mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
feat: user register (#196)
This commit is contained in:
parent
37f2bab9c0
commit
5c428efa78
4 changed files with 49 additions and 57 deletions
|
@ -21,7 +21,7 @@ const createRouters = (provider: Provider) => {
|
|||
|
||||
statusRoutes(anonymousRouter);
|
||||
sessionRoutes(anonymousRouter, provider);
|
||||
userRoutes(anonymousRouter, provider);
|
||||
userRoutes(anonymousRouter);
|
||||
swaggerRoutes(anonymousRouter);
|
||||
|
||||
const router: AuthedRouter = new Router();
|
||||
|
|
|
@ -5,8 +5,9 @@ import { Provider } from 'oidc-provider';
|
|||
import { object, string } from 'zod';
|
||||
|
||||
import RequestError from '@/errors/RequestError';
|
||||
import { encryptUserPassword, generateUserId } from '@/lib/user';
|
||||
import koaGuard from '@/middleware/koa-guard';
|
||||
import { findUserByUsername } from '@/queries/user';
|
||||
import { findUserByUsername, hasUser, insertUser } from '@/queries/user';
|
||||
import assertThat from '@/utils/assert-that';
|
||||
import { encryptPassword } from '@/utils/password';
|
||||
|
||||
|
@ -112,6 +113,48 @@ export default function sessionRoutes<T extends AnonymousRouter>(router: T, prov
|
|||
return next();
|
||||
});
|
||||
|
||||
router.post(
|
||||
'/session/register',
|
||||
koaGuard({
|
||||
body: object({
|
||||
username: string().min(3),
|
||||
password: string().min(6),
|
||||
}),
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
const { username, password } = ctx.guard.body;
|
||||
|
||||
if (await hasUser(username)) {
|
||||
throw new RequestError('user.username_exists');
|
||||
}
|
||||
|
||||
const id = await generateUserId();
|
||||
|
||||
const { passwordEncryptionSalt, passwordEncrypted, passwordEncryptionMethod } =
|
||||
encryptUserPassword(id, password);
|
||||
|
||||
await insertUser({
|
||||
id,
|
||||
username,
|
||||
passwordEncrypted,
|
||||
passwordEncryptionMethod,
|
||||
passwordEncryptionSalt,
|
||||
});
|
||||
|
||||
const redirectTo = await provider.interactionResult(
|
||||
ctx.req,
|
||||
ctx.res,
|
||||
{
|
||||
login: { accountId: id },
|
||||
},
|
||||
{ mergeWithLastSubmission: false }
|
||||
);
|
||||
ctx.body = { redirectTo };
|
||||
|
||||
return next();
|
||||
}
|
||||
);
|
||||
|
||||
router.delete('/session', async (ctx, next) => {
|
||||
await provider.interactionDetails(ctx.req, ctx.res);
|
||||
const error: LogtoErrorCode = 'oidc.aborted';
|
||||
|
|
|
@ -1,65 +1,14 @@
|
|||
import { userInfoSelectFields } from '@logto/schemas';
|
||||
import pick from 'lodash.pick';
|
||||
import { Provider } from 'oidc-provider';
|
||||
import { object, string } from 'zod';
|
||||
|
||||
import RequestError from '@/errors/RequestError';
|
||||
import { encryptUserPassword, generateUserId } from '@/lib/user';
|
||||
import { encryptUserPassword } from '@/lib/user';
|
||||
import koaGuard from '@/middleware/koa-guard';
|
||||
import {
|
||||
deleteUserById,
|
||||
findAllUsers,
|
||||
findUserById,
|
||||
hasUser,
|
||||
insertUser,
|
||||
updateUserById,
|
||||
} from '@/queries/user';
|
||||
import { deleteUserById, findAllUsers, findUserById, updateUserById } from '@/queries/user';
|
||||
|
||||
import { AnonymousRouter } from './types';
|
||||
|
||||
export default function userRoutes<T extends AnonymousRouter>(router: T, provider: Provider) {
|
||||
router.post(
|
||||
'/users',
|
||||
koaGuard({
|
||||
body: object({
|
||||
username: string().min(3),
|
||||
password: string().min(6),
|
||||
}),
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
const { username, password } = ctx.guard.body;
|
||||
|
||||
if (await hasUser(username)) {
|
||||
throw new RequestError('user.username_exists');
|
||||
}
|
||||
|
||||
const id = await generateUserId();
|
||||
|
||||
const { passwordEncryptionSalt, passwordEncrypted, passwordEncryptionMethod } =
|
||||
encryptUserPassword(id, password);
|
||||
|
||||
await insertUser({
|
||||
id,
|
||||
username,
|
||||
passwordEncrypted,
|
||||
passwordEncryptionMethod,
|
||||
passwordEncryptionSalt,
|
||||
});
|
||||
|
||||
const redirectTo = await provider.interactionResult(
|
||||
ctx.req,
|
||||
ctx.res,
|
||||
{
|
||||
login: { accountId: id },
|
||||
},
|
||||
{ mergeWithLastSubmission: false }
|
||||
);
|
||||
ctx.body = { redirectTo };
|
||||
|
||||
return next();
|
||||
}
|
||||
);
|
||||
|
||||
export default function userRoutes<T extends AnonymousRouter>(router: T) {
|
||||
router.get('/users', async (ctx, next) => {
|
||||
const users = await findAllUsers();
|
||||
ctx.body = users.map((user) => pick(user, ...userInfoSelectFields));
|
||||
|
|
|
@ -5,7 +5,7 @@ export const register = async (username: string, password: string) => {
|
|||
redirectTo: string;
|
||||
};
|
||||
return ky
|
||||
.post('/api/users', {
|
||||
.post('/api/session/register', {
|
||||
json: {
|
||||
username,
|
||||
password,
|
||||
|
|
Loading…
Reference in a new issue