2019-07-03 13:18:07 +02:00
|
|
|
import * as bcrypt from 'bcryptjs';
|
2021-08-19 14:55:45 +02:00
|
|
|
import define from '../../../define';
|
|
|
|
|
import { UserProfiles, UserSecurityKeys, Users } from '@/models/index';
|
|
|
|
|
import { publishMainStream } from '@/services/stream';
|
2019-07-03 13:18:07 +02:00
|
|
|
|
|
|
|
|
export const meta = {
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2019-07-03 13:18:07 +02:00
|
|
|
|
|
|
|
|
secure: true,
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
2019-07-03 13:18:07 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
|
|
|
|
password: { type: 'string' },
|
|
|
|
|
credentialId: { type: 'string' },
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-02-19 06:05:32 +01:00
|
|
|
required: ['password', 'credentialId'],
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2019-07-03 13:18:07 +02:00
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 06:05:32 +01:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2021-02-13 07:33:38 +01:00
|
|
|
const profile = await UserProfiles.findOneOrFail(user.id);
|
2019-07-03 13:18:07 +02: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 15:58:30 +01:00
|
|
|
id: ps.credentialId,
|
2019-07-03 13:18:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Publish meUpdated event
|
|
|
|
|
publishMainStream(user.id, 'meUpdated', await Users.pack(user.id, user, {
|
|
|
|
|
detail: true,
|
2021-12-09 15:58:30 +01:00
|
|
|
includeSecrets: true,
|
2019-07-03 13:18:07 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
});
|