2023-01-13 05:40:33 +01:00
|
|
|
import define from "../../../define.js";
|
|
|
|
import { Users } from "@/models/index.js";
|
|
|
|
import { signup } from "../../../common/signup.js";
|
|
|
|
import { IsNull } from "typeorm";
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
export const meta = {
|
2023-01-13 05:40:33 +01:00
|
|
|
tags: ["admin"],
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2021-03-06 14:34:11 +01:00
|
|
|
res: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "object",
|
|
|
|
optional: false,
|
|
|
|
nullable: false,
|
|
|
|
ref: "User",
|
2021-03-06 14:34:11 +01:00
|
|
|
properties: {
|
|
|
|
token: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "string",
|
|
|
|
optional: false,
|
|
|
|
nullable: false,
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "object",
|
2022-02-19 06:05:32 +01:00
|
|
|
properties: {
|
|
|
|
username: Users.localUsernameSchema,
|
|
|
|
password: Users.passwordSchema,
|
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
required: ["username", "password"],
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
2023-01-13 05:54:33 +01:00
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
export default define(meta, paramDef, async (ps, _me) => {
|
2022-03-26 07:34:00 +01:00
|
|
|
const me = _me ? await Users.findOneByOrFail({ id: _me.id }) : null;
|
2023-01-13 05:40:33 +01:00
|
|
|
const noUsers =
|
|
|
|
(await Users.countBy({
|
|
|
|
host: IsNull(),
|
|
|
|
})) === 0;
|
|
|
|
if (!(noUsers || me?.isAdmin)) throw new Error("access denied");
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2021-10-08 06:37:02 +02:00
|
|
|
const { account, secret } = await signup({
|
|
|
|
username: ps.username,
|
|
|
|
password: ps.password,
|
|
|
|
});
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
const res = await Users.pack(account, account, {
|
|
|
|
detail: true,
|
2021-12-09 15:58:30 +01:00
|
|
|
includeSecrets: true,
|
2020-01-29 20:37:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
(res as any).token = secret;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
});
|