2022-02-27 11:07:39 +09:00
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
|
import define from '../../../define.js';
|
|
|
|
|
import { UserProfiles, UserSecurityKeys, Users } from '@/models/index.js';
|
|
|
|
|
import { publishMainStream } from '@/services/stream.js';
|
2019-07-03 07:18:07 -04:00
|
|
|
|
|
|
|
|
export const meta = {
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: true,
|
2019-07-03 07:18:07 -04:00
|
|
|
|
|
|
|
|
secure: true,
|
2022-02-19 14:05:32 +09:00
|
|
|
} as const;
|
2019-07-03 07:18:07 -04:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
|
|
|
|
password: { type: 'string' },
|
|
|
|
|
credentialId: { type: 'string' },
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-02-19 14:05:32 +09:00
|
|
|
required: ['password', 'credentialId'],
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2019-07-03 07:18:07 -04:00
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 14:05:32 +09:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2021-02-13 15:33:38 +09:00
|
|
|
const profile = await UserProfiles.findOneOrFail(user.id);
|
2019-07-03 07:18:07 -04:00
|
|
|
|
|
|
|
|
// Compare password
|
|
|
|
|
const same = await bcrypt.compare(ps.password, profile.password!);
|
|
|
|
|
|
|
|
|
|
if (!same) {
|
|
|
|
|
throw new Error('incorrect password');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure we only delete the user's own creds
|
|
|
|
|
await UserSecurityKeys.delete({
|
|
|
|
|
userId: user.id,
|
2021-12-09 23:58:30 +09:00
|
|
|
id: ps.credentialId,
|
2019-07-03 07:18:07 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Publish meUpdated event
|
|
|
|
|
publishMainStream(user.id, 'meUpdated', await Users.pack(user.id, user, {
|
|
|
|
|
detail: true,
|
2021-12-09 23:58:30 +09:00
|
|
|
includeSecrets: true,
|
2019-07-03 07:18:07 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
});
|