mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-26 21:39:43 +02:00
64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
import { buildBackendUrl } from "@/lib/env-config";
|
|
|
|
type SessionState =
|
|
| { status: "loading"; authenticated: false; accessExpiresAt: null }
|
|
| { status: "authenticated"; authenticated: true; accessExpiresAt: number | null }
|
|
| { status: "unauthenticated"; authenticated: false; accessExpiresAt: null };
|
|
|
|
async function getSessionHeaders(): Promise<HeadersInit> {
|
|
if (typeof window === "undefined" || !window.electronAPI?.getAccessToken) {
|
|
return {};
|
|
}
|
|
|
|
const token = await window.electronAPI.getAccessToken();
|
|
return token ? { Authorization: `Bearer ${token}` } : {};
|
|
}
|
|
|
|
export function useSession() {
|
|
const [state, setState] = useState<SessionState>({
|
|
status: "loading",
|
|
authenticated: false,
|
|
accessExpiresAt: null,
|
|
});
|
|
|
|
const refresh = useCallback(async () => {
|
|
try {
|
|
const response = await fetch(buildBackendUrl("/auth/session"), {
|
|
credentials: "include",
|
|
headers: await getSessionHeaders(),
|
|
});
|
|
if (!response.ok) {
|
|
setState({
|
|
status: "unauthenticated",
|
|
authenticated: false,
|
|
accessExpiresAt: null,
|
|
});
|
|
return;
|
|
}
|
|
const data = (await response.json()) as {
|
|
authenticated: boolean;
|
|
access_expires_at: number | null;
|
|
};
|
|
setState({
|
|
status: "authenticated",
|
|
authenticated: true,
|
|
accessExpiresAt: data.access_expires_at,
|
|
});
|
|
} catch {
|
|
setState({
|
|
status: "unauthenticated",
|
|
authenticated: false,
|
|
accessExpiresAt: null,
|
|
});
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
void refresh();
|
|
}, [refresh]);
|
|
|
|
return { ...state, refresh };
|
|
}
|