Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions src/app/auth/components/ChangePassword/ChangePassword.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Dispatch, SetStateAction, useState, RefObject, createRef, useEffect } from 'react';
import { Link } from 'react-router-dom';
import authService from 'app/auth/services/auth.service';
import notificationsService, { ToastType } from 'app/notifications/services/notifications.service';
import testPasswordStrength from '@internxt/lib/dist/src/auth/testPasswordStrength';
import { useTranslationContext } from 'app/i18n/provider/TranslationProvider';
import { Button, Input } from '@internxt/ui';
import { CaretLeft, CheckCircle, FileArrowUp, Warning, WarningCircle } from '@phosphor-icons/react';
import authService from 'app/auth/services/auth.service';
import errorService from 'app/core/services/error.service';
import localStorageService from 'app/core/services/local-storage.service';
import { useTranslationContext } from 'app/i18n/provider/TranslationProvider';
import notificationsService, { ToastType } from 'app/notifications/services/notifications.service';
import PasswordStrengthIndicator from 'app/shared/components/PasswordStrengthIndicator';
import { MAX_PASSWORD_LENGTH } from 'app/shared/components/ValidPassword';
import { CaretLeft, FileArrowUp, Warning, WarningCircle, CheckCircle } from '@phosphor-icons/react';
import { validateMnemonic } from 'bip39';
import errorService from 'app/core/services/error.service';
import localStorageService from 'app/core/services/local-storage.service';
import { BackupData } from 'app/utils/backupKeyUtils';
import { Dispatch, RefObject, SetStateAction, createRef, useEffect, useState } from 'react';
import { Link } from 'react-router-dom';

interface ChangePasswordProps {
setHasBackupKey: Dispatch<SetStateAction<boolean | undefined>>;
}
Expand Down Expand Up @@ -68,6 +68,7 @@ export default function ChangePassword(props: Readonly<ChangePasswordProps>): JS

try {
const backupData = JSON.parse(uploadedBackupKeyContent);
console.log({ backupData });
if (backupData.mnemonic && validateMnemonic(backupData.mnemonic)) {
setBackupKeyContent(uploadedBackupKeyContent);
return;
Expand Down Expand Up @@ -124,18 +125,6 @@ export default function ChangePassword(props: Readonly<ChangePasswordProps>): JS
const token = window.location.pathname.split('/').pop();
const password = newPassword;

let mnemonic = backupKeyContent;
let backupData: BackupData | undefined;

try {
backupData = JSON.parse(backupKeyContent) as BackupData;
if (backupData?.mnemonic) {
mnemonic = backupData.mnemonic;
}
} catch (err) {
// plain mnemonic
}

if (!token) {
notificationsService.show({
text: translate('auth.recoverAccount.changePassword.tokenError'),
Expand All @@ -146,11 +135,7 @@ export default function ChangePassword(props: Readonly<ChangePasswordProps>): JS
}

try {
if (backupData && (backupData.privateKey || backupData.keys)) {
await authService.updateCredentialsWithToken(token, password, mnemonic, backupData);
} else {
await authService.updateCredentialsWithToken(token, password, mnemonic);
}
await authService.recoverAccountWithBackupKey(token, password, backupKeyContent);

localStorageService.clear();
setIsEmailSent(true);
Expand Down
49 changes: 48 additions & 1 deletion src/app/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ import { productsThunks } from 'app/store/slices/products';
import { referralsThunks } from 'app/store/slices/referrals';
import { initializeUserThunk, userActions, userThunks } from 'app/store/slices/user';
import { workspaceThunks } from 'app/store/slices/workspaces/workspacesStore';
import { BackupData, detectBackupKeyFormat, prepareOldBackupRecoverPayloadForBackend } from 'app/utils/backupKeyUtils';
import { generateMnemonic, validateMnemonic } from 'bip39';
import { SdkFactory } from '../../core/factory/sdk';
import envService from '../../core/services/env.service';
import errorService from '../../core/services/error.service';
import httpService from '../../core/services/http.service';
import vpnAuthService from './vpnAuth.service';
import { BackupData } from 'app/utils/backupKeyUtils';

type ProfileInfo = {
user: UserSettings;
Expand Down Expand Up @@ -237,6 +237,52 @@ export const getPasswordDetails = async (
return { salt, hashedCurrentPassword, encryptedCurrentPassword };
};

/**
* Recover account using backup key content
* This function detects the backup key format and uses appropriate recovery method
*
* @param token - The recovery token
* @param password - The new password
* @param backupKeyContent - The content of the backup key file
* @returns Promise<void>
*/
export const recoverAccountWithBackupKey = async (
token: string,
password: string,
backupKeyContent: string,
): Promise<void> => {
try {
const { type, mnemonic, backupData } = detectBackupKeyFormat(backupKeyContent);

if (type === 'old') {
return recoveryOldBackuptWithToken(token, password, mnemonic);
} else {
return updateCredentialsWithToken(token, password, mnemonic, backupData);
}
} catch (error) {
console.error('Error recovering account with backup key:', error);
throw error;
}
};

/**
* Recovery method for old backup keys format (mnemonic only)
*/
export const recoveryOldBackuptWithToken = async (token: string, password: string, mnemonic: string): Promise<void> => {
try {
const authClient = SdkFactory.getNewApiInstance().createAuthClient();
const recoverPayload = await prepareOldBackupRecoverPayloadForBackend({ mnemonic, password, token });

return authClient.legacyRecoverAccount(recoverPayload);
} catch (error) {
console.error('Error updating credentials with token:', error);
throw error;
}
};

/**
* Updates credentials with token (used for new backup format)
*/
export const updateCredentialsWithToken = async (
token: string | undefined,
newPassword: string,
Expand Down Expand Up @@ -605,6 +651,7 @@ const authService = {
requestUnblockAccount,
unblockAccount,
authenticateUser,
recoverAccountWithBackupKey,
};

export default authService;
Loading
Loading