All files / services auth.service.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 4/4
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                            10x         10x 27x           17x           10x 5x       3x           10x 12x 8x           10x     9x    
/**
 * Authentication service
 * Handles login, token refresh, and user info retrieval
 */
 
import { apiClient } from '@/lib/api';
import type {
  LoginRequest,
  AuthResponse,
  TokenRefreshResponse,
  CurrentUserResponse,
} from '@/types/auth.types';
 
// System account ID for superadmin
const SYSTEM_ACCOUNT_ID = 'SY0000';
 
/**
 * Login with superadmin credentials
 */
export const login = async (email: string, password: string): Promise<AuthResponse> => {
  const response = await apiClient.post<AuthResponse>('/auth/login', {
    account: SYSTEM_ACCOUNT_ID,
    email,
    password,
  } as LoginRequest & { account: string });
  
  return response.data;
};
 
/**
 * Refresh access token
 */
export const refreshToken = async (refreshToken: string): Promise<TokenRefreshResponse> => {
  const response = await apiClient.post<TokenRefreshResponse>('/auth/refresh', {
    refresh_token: refreshToken,
  });
  
  return response.data;
};
 
/**
 * Get current user info
 */
export const getCurrentUser = async (): Promise<CurrentUserResponse> => {
  const response = await apiClient.get<CurrentUserResponse>('/auth/me');
  return response.data;
};
 
/**
 * Logout (client-side only - clears local state)
 */
export const logout = (): void => {
  // In the future, we could call a backend logout endpoint to revoke tokens
  // For now, just clear local storage
  localStorage.removeItem('auth-storage');
};