2022-02-27 02:07:39 +00:00
|
|
|
import bcrypt from 'bcryptjs';
|
2022-07-12 12:41:10 +00:00
|
|
|
import { UserProfiles, PasswordResetRequests } from '@/models/index.js';
|
2022-06-14 09:01:23 +00:00
|
|
|
import define from '../define.js';
|
2021-05-04 06:05:34 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2022-06-10 05:25:20 +00:00
|
|
|
tags: ['reset password'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: false,
|
2021-05-04 06:05:34 +00:00
|
|
|
|
2022-06-10 05:25:20 +00:00
|
|
|
description: 'Complete the password reset that was previously requested.',
|
|
|
|
|
2022-02-19 05:05:32 +00:00
|
|
|
errors: {
|
2021-05-04 06:05:34 +00:00
|
|
|
|
|
|
|
},
|
2022-02-19 05:05:32 +00:00
|
|
|
} as const;
|
2021-05-04 06:05:34 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
token: { type: 'string' },
|
|
|
|
password: { type: 'string' },
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2022-02-19 05:05:32 +00:00
|
|
|
required: ['token', 'password'],
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2021-05-04 06:05:34 +00:00
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 05:05:32 +00:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2022-03-26 06:34:00 +00:00
|
|
|
const req = await PasswordResetRequests.findOneByOrFail({
|
2021-05-04 06:05:34 +00: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 14:58:30 +00:00
|
|
|
password: hash,
|
2021-05-04 06:05:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
PasswordResetRequests.delete(req.id);
|
|
|
|
});
|