Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

4.0 KB
Raw Blame
import { apiFetch, apiFetchJson, REFRESHED_TOKEN_HEADER } from '@platform/transport/http';

/** Keep auth transport DTO field names unchanged; session state and components own reshaping. */

const AUTH_BASE = '/api/v1/auth';

export interface UserDTO {
  user_id: string;
  email: string;
  display_name: string | null;
  is_admin: boolean;
  is_active: boolean;
  created_at: string;
  updated_at: string;
  last_login_at: string | null;
}

export interface AuthStatus {
  setup_required: boolean;
  multiuser_enabled: boolean;
  strict_password_checking: boolean;
  admin_email: string | null;
  has_single_user_work: boolean;
}

export interface LoginRequest {
  email: string;
  password: string;
  remember_me: boolean;
}

export interface LoginResult {
  token: string;
  user: UserDTO;
  expires_in: number;
}

export interface SetupRequest {
  email: string;
  display_name: string | null;
  password: string;
}

export interface ProfileUpdateRequest {
  display_name?: string;
  current_password?: string;
  new_password?: string;
}

export interface UserCreateRequest {
  email: string;
  display_name: string | null;
  password: string;
  is_admin: boolean;
}

export interface UserUpdateRequest {
  display_name?: string;
  password?: string;
  is_admin?: boolean;
  is_active?: boolean;
}

export const getAuthStatus = (): Promise<AuthStatus> => apiFetchJson<AuthStatus>(`${AUTH_BASE}/status`);

export const login = (request: LoginRequest, signal?: AbortSignal): Promise<LoginResult> =>
  apiFetchJson<LoginResult>(`${AUTH_BASE}/login`, { body: JSON.stringify(request), method: 'POST', signal });

export const logout = (): Promise<{ success: boolean }> =>
  apiFetchJson<{ success: boolean }>(`${AUTH_BASE}/logout`, { method: 'POST' });

/**
 * Restore the media cookie for stored-token sessions before elements load; they cannot send bearer headers. Single-user
 * backends safely no-op.
 */
export const refreshMediaCookie = (signal?: AbortSignal): Promise<{ success: boolean }> =>
  apiFetchJson<{ success: boolean }>(`${AUTH_BASE}/media-cookie`, { method: 'POST', signal });

export const getCurrentUser = (signal?: AbortSignal): Promise<UserDTO> =>
  apiFetchJson<UserDTO>(`${AUTH_BASE}/me`, { signal });

export const setupAdmin = (request: SetupRequest): Promise<{ success: boolean; user: UserDTO }> =>
  apiFetchJson<{ success: boolean; user: UserDTO }>(`${AUTH_BASE}/setup`, {
    body: JSON.stringify(request),
    method: 'POST',
  });

/** An edited user plus the replacement token the backend returns for the caller's own credential. */
export interface UserUpdateResult {
  refreshedToken: string | null;
  user: UserDTO;
}

const patchUser = async (path: string, changes: object, signal?: AbortSignal): Promise<UserUpdateResult> => {
  const response = await apiFetch(path, {
    body: JSON.stringify(changes),
    headers: { 'Content-Type': 'application/json' },
    method: 'PATCH',
    signal,
  });

  return { refreshedToken: response.headers.get(REFRESHED_TOKEN_HEADER), user: (await response.json()) as UserDTO };
};

export const updateCurrentUser = (request: ProfileUpdateRequest, signal?: AbortSignal): Promise<UserUpdateResult> =>
  patchUser(`${AUTH_BASE}/me`, request, signal);

export const listUsers = (): Promise<UserDTO[]> => apiFetchJson<UserDTO[]>(`${AUTH_BASE}/users`);

export const createUser = (request: UserCreateRequest): Promise<UserDTO> =>
  apiFetchJson<UserDTO>(`${AUTH_BASE}/users`, { body: JSON.stringify(request), method: 'POST' });

export const updateUser = (
  userId: string,
  changes: UserUpdateRequest,
  signal?: AbortSignal
): Promise<UserUpdateResult> => patchUser(`${AUTH_BASE}/users/${encodeURIComponent(userId)}`, changes, signal);

export const deleteUser = async (userId: string): Promise<void> => {
  await apiFetch(`${AUTH_BASE}/users/${encodeURIComponent(userId)}`, { method: 'DELETE' });
};

export const generatePassword = async (): Promise<string> => {
  const body = await apiFetchJson<{ password: string }>(`${AUTH_BASE}/generate-password`);

  return body.password;
};