All files / src/api auth.ts

100% Statements 93/93
100% Branches 33/33
100% Functions 14/14
100% Lines 93/93

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 1141x   1x 1x   1x   15x 15x 1x 1x 15x 15x   1x 1x 1x   3x 3x 3x 3x 3x 3x   3x 3x 3x 3x 3x   1x 2x 2x   1x 7x 7x   1x 4x 4x 4x   1x 4x 4x 3x 3x   2x 2x 2x 2x 2x 2x 1x 1x 1x   3x 3x 3x 3x 2x 2x   1x 1x 1x 1x   4x 4x 4x 1x 1x   3x 3x 3x   4x 2x 2x 2x   1x 1x 1x   2x 2x 2x 1x 1x   2x 2x 2x 1x 1x   2x 2x 2x 2x 2x 2x 2x 2x 1x 1x  
import { createClient, type Session } from "@supabase/supabase-js";
 
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL;
const supabaseKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
 
let _supabase: ReturnType<typeof createClient> | null = null;
 
function getSupabase() {
  if (!_supabase) {
    _supabase = createClient(supabaseUrl, supabaseKey);
  }
  return _supabase;
}
 
const TOKEN_KEY = "access_token";
const REFRESH_KEY = "refresh_token";
const EXPIRES_KEY = "token_expires_at";
 
function saveSession(session: Session) {
  localStorage.setItem(TOKEN_KEY, session.access_token);
  localStorage.setItem(REFRESH_KEY, session.refresh_token);
  const expiresAt = Date.now() + session.expires_in * 1000;
  localStorage.setItem(EXPIRES_KEY, String(expiresAt));
}
 
function clearSession() {
  localStorage.removeItem(TOKEN_KEY);
  localStorage.removeItem(REFRESH_KEY);
  localStorage.removeItem(EXPIRES_KEY);
}
 
export function getToken(): string | null {
  return localStorage.getItem(TOKEN_KEY);
}
 
export function getRefreshToken(): string | null {
  return localStorage.getItem(REFRESH_KEY);
}
 
export function getTokenExpiresAt(): number | null {
  const raw = localStorage.getItem(EXPIRES_KEY);
  return raw ? Number(raw) : null;
}
 
export function isTokenExpiringSoon(bufferMs: number = 5 * 60 * 1000): boolean {
  const expiresAt = getTokenExpiresAt();
  if (!expiresAt) return false;
  return Date.now() >= expiresAt - bufferMs;
}
 
export async function signIn(email: string, password: string) {
  const { data, error } = await getSupabase().auth.signInWithPassword({
    email,
    password,
  });
  if (error) throw error;
  if (data.session) saveSession(data.session);
  return data.user;
}
 
export async function signUp(email: string, password: string) {
  const { data, error } = await getSupabase().auth.signUp({ email, password });
  if (error) throw error;
  if (data.session) saveSession(data.session);
  return data.user;
}
 
export async function signOut() {
  await getSupabase().auth.signOut();
  clearSession();
}
 
export async function refreshSession(): Promise<string> {
  const refreshToken = getRefreshToken();
  if (!refreshToken) {
    throw new Error("No refresh token available");
  }
 
  const { data, error } = await getSupabase().auth.refreshSession({
    refresh_token: refreshToken,
  });
 
  if (error || !data.session) {
    clearSession();
    throw new Error(error?.message || "Session refresh failed");
  }
 
  saveSession(data.session);
  return data.session.access_token;
}
 
export async function updateEmail(email: string) {
  const { data, error } = await getSupabase().auth.updateUser({ email });
  if (error) throw error;
  return data;
}
 
export async function updatePassword(password: string) {
  const { data, error } = await getSupabase().auth.updateUser({ password });
  if (error) throw error;
  return data;
}
 
export async function requestPasswordReset(email: string) {
  const { data, error } = await getSupabase().auth.resetPasswordForEmail(
    email,
    {
      redirectTo: `${window.location.origin}/settings`,
    },
  );
  if (error) throw error;
  return data;
}