Skip to content

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

5.0 KB
Raw Blame
import React, { createContext, useContext, useState, useMemo, useCallback } from 'react';

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

import { StorageKeys } from '@/utils/constants/storageKeys';
import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
import { sendMessage } from '@/utils/messaging/ExtensionMessaging';

import { storage } from '#imports';

type AuthContextType = {
  isInitialized: boolean;
  username: string | null;
  initializeAuth: () => Promise<boolean>;
  setAuthTokens: (username: string, accessToken: string, refreshToken: string) => Promise<void>;
  clearAuthUserInitiated: (errorMessage?: string) => Promise<void>;
  clearAuthForced: (errorMessage?: string) => Promise<void>;
  globalMessage: string | null;
  clearGlobalMessage: () => void;
}

/**
 * Auth context.
 */
const AuthContext = createContext<AuthContextType | undefined>(undefined);

/**
 * AuthProvider to provide the authentication state to the app that components can use.
 */
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const [isInitialized, setIsInitialized] = useState(false);
  const [username, setUsername] = useState<string | null>(null);
  const [globalMessage, setGlobalMessage] = useState<string | null>(null);
  const dbContext = useDb();

  /**
   * Initialize the authentication state.
   *
   * @returns boolean indicating whether the user is logged in.
   */
  const initializeAuth = useCallback(async () : Promise<boolean> => {
    const accessToken = await storage.getItem(StorageKeys.ACCESS_TOKEN) as string;
    const refreshToken = await storage.getItem(StorageKeys.REFRESH_TOKEN) as string;
    const username = await storage.getItem(StorageKeys.USERNAME) as string;
    setIsInitialized(true);
    if (accessToken && refreshToken && username) {
      setUsername(username);
      return true;
    }

    return false;
  }, [setUsername]);

  /**
   * Set auth tokens in browser local storage as part of the login process. After db is initialized, the login method should be called as well.
   */
  const setAuthTokens = useCallback(async (username: string, accessToken: string, refreshToken: string) : Promise<void> => {
    await storage.setItem(StorageKeys.USERNAME, username);
    await storage.setItem(StorageKeys.ACCESS_TOKEN, accessToken);
    await storage.setItem(StorageKeys.REFRESH_TOKEN, refreshToken);

    // Clear dismiss until (which can be enabled after user has dimissed vault is locked popup) to ensure popup is shown.
    await LocalPreferencesService.setVaultLockedDismissUntil(0);

    setUsername(username);
  }, []);

  /**
   * Clear authentication data and tokens from storage (forced logout).
   * This is called when the server forces a logout (401, token revocation, password change).
   * Clears the encrypted vault and everything derived from it; the next login pulls a fresh one.
   * Keeps username for login page prefill, and the local preferences.
   *
   * This is the base logout function. clearAuthUserInitiated builds on top of this.
   *
   * @param errorMessage Optional error message to display on the login page
   */
  const clearAuthForced = useCallback(async (errorMessage?: string) : Promise<void> => {
    // Clear session data: tokens, ephemeral data and the vault itself
    await sendMessage('CLEAR_SESSION');

    // Clear in-memory database reference
    dbContext?.clearDatabase();

    // Set global message that will be shown on the login page
    if (errorMessage) {
      setGlobalMessage(errorMessage);
    }
  }, [dbContext]);

  /**
   * Clear authentication data and tokens from storage (user-initiated logout).
   * This is called when the user explicitly clicks the logout button.
   *
   * Builds on clearAuthForced by also clearing the username and the local preferences.
   *
   * @param errorMessage Optional error message to display on the login page
   */
  const clearAuthUserInitiated = useCallback(async (errorMessage?: string) : Promise<void> => {
    // Clear local vault data explicitly.
    await sendMessage('CLEAR_VAULT_DATA');

    // Run the parent forced logout which clears everything else.
    await clearAuthForced(errorMessage);

    setUsername(null);
  }, [clearAuthForced]);

  /**
   * Clear global message (called after displaying the message).
   */
  const clearGlobalMessage = useCallback(() : void => {
    setGlobalMessage(null);
  }, []);

  const contextValue = useMemo(() => ({
    isInitialized,
    username,
    initializeAuth,
    setAuthTokens,
    clearAuthUserInitiated,
    clearAuthForced,
    globalMessage,
    clearGlobalMessage,
  }), [isInitialized, username, initializeAuth, globalMessage, setAuthTokens, clearAuthUserInitiated, clearAuthForced, clearGlobalMessage]);

  return (
    <AuthContext.Provider value={contextValue}>
      {children}
    </AuthContext.Provider>
  );
};

/**
 * Hook to use the AuthContext
 */
export const useAuth = () : AuthContextType => {
  const context = useContext(AuthContext);
  if (context === undefined) {
    throw new Error('useAuth must be used within an AuthProvider');
  }
  return context;
};