b9cb6d1c10
将来ESMに移行しやすいように Related: #7658 なんかmochaが起動しなくなってるけど理由不明 すぐ直したい
46 lines
958 B
TypeScript
46 lines
958 B
TypeScript
import $ from 'cafy';
|
|
import * as 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';
|
|
|
|
export const meta = {
|
|
requireCredential: false as const,
|
|
|
|
params: {
|
|
token: {
|
|
validator: $.str
|
|
},
|
|
|
|
password: {
|
|
validator: $.str
|
|
}
|
|
},
|
|
|
|
errors: {
|
|
|
|
}
|
|
};
|
|
|
|
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, {
|
|
password: hash
|
|
});
|
|
|
|
PasswordResetRequests.delete(req.id);
|
|
});
|