mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-28 21:49:40 +02:00
- Introduced fetchSession function to streamline session fetching logic. - Updated useSession to handle 401 errors by refreshing the session when necessary. - Modified getDesktopAccessToken to accept options for forced token refresh, improving desktop authentication flow.
75 lines
1.8 KiB
TypeScript
75 lines
1.8 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
import { refreshSession } from "@/lib/auth-utils";
|
|
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}` } : {};
|
|
}
|
|
|
|
async function fetchSession(): Promise<Response> {
|
|
return fetch(buildBackendUrl("/auth/session"), {
|
|
credentials: "include",
|
|
headers: await getSessionHeaders(),
|
|
});
|
|
}
|
|
|
|
export function useSession() {
|
|
const [state, setState] = useState<SessionState>({
|
|
status: "loading",
|
|
authenticated: false,
|
|
accessExpiresAt: null,
|
|
});
|
|
|
|
const refresh = useCallback(async () => {
|
|
try {
|
|
let response = await fetchSession();
|
|
if (response.status === 401) {
|
|
const refreshed = await refreshSession();
|
|
if (refreshed) {
|
|
response = await fetchSession();
|
|
}
|
|
}
|
|
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 };
|
|
}
|