mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-26 21:39:43 +02:00
feat: enhance session handling in useSession and auth-fetch
- 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.
This commit is contained in:
parent
23c128dd0d
commit
013fae6eba
2 changed files with 24 additions and 7 deletions
|
|
@ -1,6 +1,7 @@
|
|||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { refreshSession } from "@/lib/auth-utils";
|
||||
import { buildBackendUrl } from "@/lib/env-config";
|
||||
|
||||
type SessionState =
|
||||
|
|
@ -17,6 +18,13 @@ async function getSessionHeaders(): Promise<HeadersInit> {
|
|||
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",
|
||||
|
|
@ -26,10 +34,13 @@ export function useSession() {
|
|||
|
||||
const refresh = useCallback(async () => {
|
||||
try {
|
||||
const response = await fetch(buildBackendUrl("/auth/session"), {
|
||||
credentials: "include",
|
||||
headers: await getSessionHeaders(),
|
||||
});
|
||||
let response = await fetchSession();
|
||||
if (response.status === 401) {
|
||||
const refreshed = await refreshSession();
|
||||
if (refreshed) {
|
||||
response = await fetchSession();
|
||||
}
|
||||
}
|
||||
if (!response.ok) {
|
||||
setState({
|
||||
status: "unauthenticated",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue