2023-01-13 05:40:33 +01:00
|
|
|
import bcrypt from "bcryptjs";
|
|
|
|
import { publishMainStream } from "@/services/stream.js";
|
|
|
|
import { Users, UserProfiles, PasswordResetRequests } from "@/models/index.js";
|
|
|
|
import define from "../define.js";
|
|
|
|
import { ApiError } from "../error.js";
|
2021-05-04 08:05:34 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2023-01-13 05:40:33 +01:00
|
|
|
tags: ["reset password"],
|
2022-06-10 07:25:20 +02:00
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2021-05-04 08:05:34 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
description: "Complete the password reset that was previously requested.",
|
2022-06-10 07:25:20 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
errors: {},
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
2021-05-04 08:05:34 +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
|
|
|
token: { type: "string" },
|
|
|
|
password: { type: "string" },
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
required: ["token", "password"],
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2021-05-04 08:05:34 +02:00
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2022-03-26 07:34:00 +01:00
|
|
|
const req = await PasswordResetRequests.findOneByOrFail({
|
2021-05-04 08:05:34 +02:00
|
|
|
token: ps.token,
|
|
|
|
});
|
|
|
|
|
|
|
|
// 発行してから30分以上経過していたら無効
|
|
|
|
if (Date.now() - req.createdAt.getTime() > 1000 * 60 * 30) {
|
|
|
|
throw new Error(); // TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate hash of password
|
|
|
|
const salt = await bcrypt.genSalt(8);
|
|
|
|
const hash = await bcrypt.hash(ps.password, salt);
|
|
|
|
|
|
|
|
await UserProfiles.update(req.userId, {
|
2021-12-09 15:58:30 +01:00
|
|
|
password: hash,
|
2021-05-04 08:05:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
PasswordResetRequests.delete(req.id);
|
|
|
|
});
|