summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/i/update-email.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/server/api/endpoints/i/update-email.ts')
-rw-r--r--packages/backend/src/server/api/endpoints/i/update-email.ts20
1 files changed, 17 insertions, 3 deletions
diff --git a/packages/backend/src/server/api/endpoints/i/update-email.ts b/packages/backend/src/server/api/endpoints/i/update-email.ts
index 77135bf855..a36b3a732b 100644
--- a/packages/backend/src/server/api/endpoints/i/update-email.ts
+++ b/packages/backend/src/server/api/endpoints/i/update-email.ts
@@ -14,6 +14,7 @@ import type { Config } from '@/config.js';
import { DI } from '@/di-symbols.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { L_CHARS, secureRndstr } from '@/misc/secure-rndstr.js';
+import { UserAuthService } from '@/core/UserAuthService.js';
import { ApiError } from '../../error.js';
export const meta = {
@@ -46,6 +47,7 @@ export const paramDef = {
properties: {
password: { type: 'string' },
email: { type: 'string', nullable: true },
+ token: { type: 'string', nullable: true },
},
required: ['password'],
} as const;
@@ -61,15 +63,27 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private userEntityService: UserEntityService,
private emailService: EmailService,
+ private userAuthService: UserAuthService,
private globalEventService: GlobalEventService,
) {
super(meta, paramDef, async (ps, me) => {
+ const token = ps.token;
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: me.id });
- // Compare password
- const same = await bcrypt.compare(ps.password, profile.password!);
+ if (profile.twoFactorEnabled) {
+ if (token == null) {
+ throw new Error('authentication failed');
+ }
+
+ try {
+ await this.userAuthService.twoFactorAuthenticate(profile, token);
+ } catch (e) {
+ throw new Error('authentication failed');
+ }
+ }
- if (!same) {
+ const passwordMatched = await bcrypt.compare(ps.password, profile.password!);
+ if (!passwordMatched) {
throw new ApiError(meta.errors.incorrectPassword);
}