Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

2.2 KB
Raw Blame
import { AppErrorCode, getAppErrorCode } from '@aliasvault/client/api/errors/AppErrorCodes';
import { VaultVersionIncompatibleError } from '@aliasvault/client/api/errors/VaultVersionIncompatibleError';
import { describeAuthError, formatErrorMessage } from '@aliasvault/client/auth/AuthErrorMessage';
import { VaultKeyService } from '@aliasvault/client/auth/VaultKeyService';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';

import { useApp } from '@/entrypoints/popup/context/AppContext';
import { useDb } from '@/entrypoints/popup/context/DbContext';

import { logFailure } from '@/utils/Diagnostics';

/**
 * Finish a login or unlock the background completed: load the vault it unlocked and continue.
 */
export function useFinishBackgroundAuth(): (offline: boolean) => Promise<string | null> {
  const { t } = useTranslation();
  const app = useApp();
  const dbContext = useDb();
  const navigate = useNavigate();

  return useCallback(async (offline: boolean): Promise<string | null> => {
    try {
      await app.initializeAuth();
      await dbContext.setIsOffline(offline);

      const sqliteClient = await dbContext.loadStoredDatabase();
      if (sqliteClient && await sqliteClient.requiresLegacySqliteBlobMigration()) {
        navigate('/upgrade', { replace: true });
        return null;
      }

      // Reinitialize syncs with the server and routes to the page to show.
      navigate('/reinitialize', { replace: true });
      return null;
    } catch (err) {
      if (err instanceof VaultVersionIncompatibleError) {
        await app.logout(err.message);
        return null;
      }
      if (getAppErrorCode(err) === AppErrorCode.VAULT_DECRYPT_FAILED && await VaultKeyService.hasLocalVaultKey()) {
        // The key chain opened but the stored vault does not decrypt; the next login pulls the vault again.
        await app.logout(t('common.errors.sessionExpired'));
        return null;
      }
      logFailure('Loading the unlocked vault failed', err);
      return formatErrorMessage(await describeAuthError(err, { fallback: 'common.errors.unknownErrorTryAgain' }), t);
    }
  }, [app, dbContext, navigate, t]);
}