← back to Bertha

kalshi-dash/src/api.js

46 lines

const API = '/api';

export async function api(path, opts = {}) {
  const res = await fetch(`${API}${path}`, {
    ...opts,
    credentials: 'same-origin',
    headers: { 'Content-Type': 'application/json', ...opts.headers },
  });
  if (res.status === 401 && !path.startsWith('/auth/')) {
    window.location.href = '/login';
    return {};
  }
  return res.json();
}

export async function post(path, body) {
  return api(path, { method: 'POST', body: JSON.stringify(body) });
}

export async function checkSession() {
  try {
    const data = await api('/auth/session');
    return data.authenticated === true;
  } catch { return false; }
}

export async function login(username, password) {
  const data = await post('/auth/login', { username, password });
  return data.success === true;
}

export async function googleLogin(credential) {
  const data = await post('/auth/google', { credential });
  return data;
}

export async function getGoogleConfig() {
  try { return await api('/auth/google-config'); }
  catch { return { configured: false }; }
}

export async function logout() {
  await post('/auth/logout', {});
  window.location.href = '/login';
}