Files
TehRiehlBudget/tehriehlbudget-frontend/src/lib/supabase.ts
T
TehRiehlDeal 8c10124272
CI / test (push) Successful in 25s
CI / lint (push) Successful in 28s
CI / secrets-scan (push) Successful in 5s
CI / vuln-scan (push) Successful in 12s
CI / sast (push) Successful in 10s
CI / build-images (push) Failing after 51s
CI / image-scan (push) Has been skipped
CI / push (push) Has been skipped
Build, scan, and push images to Harbor on every main push
Wires up the CD half of the pipeline. New jobs build multi-stage Docker
images for the frontend and backend, run a Trivy image scan that fails
on HIGH/CRITICAL findings, and push to harbor.tehriehldeal.com on main
only. Each push tags <version> (from package.json), <sha>, and latest;
a pre-push existence check refuses to overwrite a version tag that
already points at a different digest, forcing a real bump.

The Vite frontend now reads runtime config from window.__RUNTIME_CONFIG__,
populated by /config.js which nginx renders from container env vars at
startup via envsubst. A getConfig() helper falls back to import.meta.env
for `pnpm dev` and Vitest, so existing test scaffolding keeps working.
PWA workbox excludes /config.js from precache and serves it NetworkOnly
to keep stale config from surviving a container restart.

Bumps frontend 0.0.0→0.1.0 and backend 0.0.1→0.1.0 (production
deployment is a meaningful new capability for both packages).

Also fixes four pre-existing tsc -b errors that the new vite build step
in the frontend Dockerfile would otherwise hit: global.fetch →
globalThis.fetch in three test files, null-guard in Activity.tsx
account filter, type cast on Recharts Pie onClick in Dashboard.tsx,
typed callback signature on the auth.test.ts onAuthStateChange mock.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 15:49:01 -07:00

57 lines
1.9 KiB
TypeScript

import { createClient } from '@supabase/supabase-js';
import { getConfig } from './runtime-config';
const supabaseUrl = getConfig('VITE_SUPABASE_URL');
const supabaseAnonKey = getConfig('VITE_SUPABASE_ANON_KEY');
export const STAY_LOGGED_IN_KEY = 'auth:stay_logged_in';
/**
* Returns true if the user opted to persist sessions across browser restarts.
* Defaults to true when no preference has been set yet.
*/
export function getStayLoggedIn(): boolean {
if (typeof window === 'undefined') return true;
const stored = window.localStorage.getItem(STAY_LOGGED_IN_KEY);
return stored === null ? true : stored === 'true';
}
export function setStayLoggedIn(value: boolean) {
if (typeof window === 'undefined') return;
window.localStorage.setItem(STAY_LOGGED_IN_KEY, String(value));
}
/**
* Storage adapter that writes Supabase session tokens to either localStorage
* (persistent across browser restarts) or sessionStorage (cleared on close),
* based on the user's "Stay Logged In" preference. Reads fall back between
* both so the session is discoverable after a preference change.
*/
const authStorage = {
getItem: (key: string) => {
if (typeof window === 'undefined') return null;
return window.localStorage.getItem(key) ?? window.sessionStorage.getItem(key);
},
setItem: (key: string, value: string) => {
if (typeof window === 'undefined') return;
const target = getStayLoggedIn() ? window.localStorage : window.sessionStorage;
const other = getStayLoggedIn() ? window.sessionStorage : window.localStorage;
target.setItem(key, value);
other.removeItem(key);
},
removeItem: (key: string) => {
if (typeof window === 'undefined') return;
window.localStorage.removeItem(key);
window.sessionStorage.removeItem(key);
},
};
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
auth: {
storage: authStorage,
persistSession: true,
autoRefreshToken: true,
detectSessionInUrl: true,
},
});