summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/admin/accounts/create.ts
blob: bceb210a82a0658a6f856a45d85701af41d369a6 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
import define from '../../../define';
import { Users } from '../../../../../models';
import { signup } from '../../../common/signup';

export const meta = {
	tags: ['admin'],

	params: {
		username: {
			validator: Users.validateLocalUsername,
		},

		password: {
			validator: Users.validatePassword,
		}
	},

	res: {
		type: 'object' as const,
		optional: false as const, nullable: false as const,
		ref: 'User',
		properties: {
			token: {
				type: 'string' as const,
				optional: false as const, nullable: false as const,
			}
		}
	}
};

export default define(meta, async (ps, _me) => {
	const me = _me ? await Users.findOneOrFail(_me.id) : null;
	const noUsers = (await Users.count({
		host: null,
	})) === 0;
	if (!noUsers && !me?.isAdmin) throw new Error('access denied');

	const { account, secret } = await signup(ps.username, ps.password);

	const res = await Users.pack(account, account, {
		detail: true,
		includeSecrets: true
	});

	(res as any).token = secret;

	return res;
});