2023-01-13 05:40:33 +01:00
|
|
|
import define from "../../../define.js";
|
2023-06-16 01:12:32 +02:00
|
|
|
import { Users, UserProfiles, UserSecurityKeys } from "@/models/index.js";
|
|
|
|
import { publishMainStream } from "@/services/stream.js";
|
|
|
|
import { ApiError } from "../../../error.js";
|
2019-07-06 18:38:36 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2019-07-06 18:38:36 +02:00
|
|
|
|
|
|
|
secure: true,
|
2023-06-16 01:12:32 +02:00
|
|
|
|
|
|
|
errors: {
|
|
|
|
noKey: {
|
|
|
|
message: "No security key.",
|
|
|
|
code: "NO_SECURITY_KEY",
|
|
|
|
id: "f9c54d7f-d4c2-4d3c-9a8g-a70daac86512",
|
|
|
|
},
|
|
|
|
},
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
2019-07-06 18:38:36 +02: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: {
|
2023-01-13 05:40:33 +01:00
|
|
|
value: { type: "boolean" },
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
required: ["value"],
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2019-07-06 18:38:36 +02:00
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2023-06-16 01:12:32 +02:00
|
|
|
if (ps.value === true) {
|
|
|
|
// セキュリティキーがなければパスワードレスを有効にはできない
|
|
|
|
const keyCount = await UserSecurityKeys.count({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
lastUsed: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (keyCount === 0) {
|
|
|
|
await UserProfiles.update(user.id, {
|
|
|
|
usePasswordLessLogin: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
throw new ApiError(meta.errors.noKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 18:38:36 +02:00
|
|
|
await UserProfiles.update(user.id, {
|
2021-12-09 15:58:30 +01:00
|
|
|
usePasswordLessLogin: ps.value,
|
2019-07-06 18:38:36 +02:00
|
|
|
});
|
2023-06-16 01:12:32 +02:00
|
|
|
|
|
|
|
const iObj = await Users.pack(user.id, user, {
|
|
|
|
detail: true,
|
|
|
|
includeSecrets: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
publishMainStream(user.id, "meUpdated", iObj);
|
2019-07-06 18:38:36 +02:00
|
|
|
});
|