2021-05-04 08:05:34 +02:00
|
|
|
import $ from 'cafy';
|
|
|
|
import * as bcrypt from 'bcryptjs';
|
2021-08-19 14:55:45 +02:00
|
|
|
import { publishMainStream } from '@/services/stream';
|
|
|
|
import define from '../define';
|
|
|
|
import { Users, UserProfiles, PasswordResetRequests } from '@/models/index';
|
|
|
|
import { ApiError } from '../error';
|
2021-05-04 08:05:34 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
requireCredential: false as const,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
token: {
|
2021-12-09 15:58:30 +01:00
|
|
|
validator: $.str,
|
2021-05-04 08:05:34 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
password: {
|
2021-12-09 15:58:30 +01:00
|
|
|
validator: $.str,
|
|
|
|
},
|
2021-05-04 08:05:34 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2021-05-04 08:05:34 +02:00
|
|
|
};
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2021-05-04 08:05:34 +02:00
|
|
|
export default define(meta, async (ps, user) => {
|
|
|
|
const req = await PasswordResetRequests.findOneOrFail({
|
|
|
|
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);
|
|
|
|
});
|