summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/private/signup-pending.ts
blob: e5e39ba00ddeab5958a2acd5e026635bf77ab27b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Koa from 'koa';
import { Users, UserPendings, UserProfiles } from '@/models/index.js';
import { signup } from '../common/signup.js';
import signin from '../common/signin.js';

export default async (ctx: Koa.Context) => {
	const body = ctx.request.body;

	const code = body['code'];

	try {
		const pendingUser = await UserPendings.findOneByOrFail({ code });

		const { account, secret } = await signup({
			username: pendingUser.username,
			passwordHash: pendingUser.password,
		});

		UserPendings.delete({
			id: pendingUser.id,
		});

		const profile = await UserProfiles.findOneByOrFail({ userId: account.id });

		await UserProfiles.update({ userId: profile.userId }, {
			email: pendingUser.email,
			emailVerified: true,
			emailVerifyCode: null,
		});

		signin(ctx, account);
	} catch (e) {
		ctx.throw(400, e);
	}
};