// src/services/tokenStorage.ts

/**
 * A centralized module for handling authentication token storage.
 * This abstraction layer makes it easy to change the storage mechanism
 * (e.g., from localStorage to sessionStorage or an in-memory store for testing)
 * without altering the application's authentication logic.
 */

const TOKEN_KEY = 'authToken';

/**
 * Retrieves the authentication token from storage.
 * @returns The token string, or null if not found or if storage is unavailable.
 */
export const getToken = (): string | null => {
  try {
    return window.localStorage.getItem(TOKEN_KEY);
  } catch (error) {
    console.error('SecurityError: Failed to access localStorage to get token.', error);
    return null;
  }
};

/**
 * Stores the authentication token.
 * @param token The token string to store.
 */
export const setToken = (token: string): void => {
  try {
    window.localStorage.setItem(TOKEN_KEY, token);
  } catch (error) {
    console.error('SecurityError: Failed to access localStorage to set token.', error);
  }
};

/**
 * Removes the authentication token from storage.
 */
export const removeToken = (): void => {
  try {
    window.localStorage.removeItem(TOKEN_KEY);
  } catch (error) {
    console.error('SecurityError: Failed to access localStorage to remove token.', error);
  }
};