2022-02-26 20:07:39 -06:00
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
import { publishMainStream } from '@/services/stream.js';
|
|
|
|
import define from '../define.js';
|
|
|
|
import { Users, UserProfiles, PasswordResetRequests } from '@/models/index.js';
|
|
|
|
import { ApiError } from '../error.js';
|
2021-05-04 01:05:34 -05:00
|
|
|
|
|
|
|
export const meta = {
|
2022-01-18 07:27:10 -06:00
|
|
|
requireCredential: false,
|
2021-05-04 01:05:34 -05:00
|
|
|
|
2022-02-18 23:05:32 -06:00
|
|
|
errors: {
|
2021-05-04 01:05:34 -05:00
|
|
|
|
|
|
|
},
|
2022-02-18 23:05:32 -06:00
|
|
|
} as const;
|
2021-05-04 01:05:34 -05:00
|
|
|
|
2022-02-19 22:15:40 -06:00
|
|
|
export const paramDef = {
|
2022-02-18 23:05:32 -06:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
token: { type: 'string' },
|
|
|
|
password: { type: 'string' },
|
2021-12-09 08:58:30 -06:00
|
|
|
},
|
2022-02-18 23:05:32 -06:00
|
|
|
required: ['token', 'password'],
|
2022-01-18 07:27:10 -06:00
|
|
|
} as const;
|
2021-05-04 01:05:34 -05:00
|
|
|
|
2022-01-02 11:12:50 -06:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-18 23:05:32 -06:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2022-03-26 01:34:00 -05:00
|
|
|
const req = await PasswordResetRequests.findOneByOrFail({
|
2021-05-04 01:05:34 -05: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 08:58:30 -06:00
|
|
|
password: hash,
|
2021-05-04 01:05:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
PasswordResetRequests.delete(req.id);
|
|
|
|
});
|