mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-22 23:31:12 +02:00
refactor(auth): route session and zero context through shared fetch
This commit is contained in:
parent
9fc93e5e82
commit
4c257e9122
1 changed files with 6 additions and 38 deletions
|
|
@ -8,7 +8,7 @@ import {
|
||||||
import { usePathname } from "next/navigation";
|
import { usePathname } from "next/navigation";
|
||||||
import { useEffect, useMemo, useRef, useState } from "react";
|
import { useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { useSession } from "@/hooks/use-session";
|
import { useSession } from "@/hooks/use-session";
|
||||||
import { getDesktopAccessToken } from "@/lib/auth-fetch";
|
import { authenticatedFetch, getDesktopAccessToken } from "@/lib/auth-fetch";
|
||||||
import { handleUnauthorized, isPublicRoute, refreshSession } from "@/lib/auth-utils";
|
import { handleUnauthorized, isPublicRoute, refreshSession } from "@/lib/auth-utils";
|
||||||
import { buildBackendUrl } from "@/lib/env-config";
|
import { buildBackendUrl } from "@/lib/env-config";
|
||||||
import type { Context } from "@/types/zero";
|
import type { Context } from "@/types/zero";
|
||||||
|
|
@ -31,46 +31,14 @@ function getCacheURL() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchZeroContext(isDesktop: boolean): Promise<LoadedZeroContext | null> {
|
async function fetchZeroContext(isDesktop: boolean): Promise<LoadedZeroContext | null> {
|
||||||
const buildHeaders = async (
|
const response = await authenticatedFetch(buildBackendUrl("/zero/context"), {
|
||||||
forceRefresh = false
|
skipAuthRedirect: true,
|
||||||
): Promise<{ headers: HeadersInit; desktopAuth?: string } | null> => {
|
});
|
||||||
const headers: HeadersInit = {};
|
|
||||||
|
|
||||||
if (isDesktop) {
|
|
||||||
const token = await getDesktopAccessToken({ forceRefresh });
|
|
||||||
if (!token) return null;
|
|
||||||
headers.Authorization = `Bearer ${token}`;
|
|
||||||
return { headers, desktopAuth: token };
|
|
||||||
}
|
|
||||||
|
|
||||||
return { headers };
|
|
||||||
};
|
|
||||||
|
|
||||||
const request = async (forceRefresh = false) => {
|
|
||||||
const auth = await buildHeaders(forceRefresh);
|
|
||||||
if (!auth) return null;
|
|
||||||
const response = await fetch(buildBackendUrl("/zero/context"), {
|
|
||||||
credentials: "include",
|
|
||||||
headers: auth.headers,
|
|
||||||
});
|
|
||||||
return { response, desktopAuth: auth.desktopAuth };
|
|
||||||
};
|
|
||||||
|
|
||||||
let result = await request();
|
|
||||||
if (result?.response.status === 401) {
|
|
||||||
const refreshed = await refreshSession();
|
|
||||||
if (refreshed) {
|
|
||||||
result = await request(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!result) return null;
|
|
||||||
const { response, desktopAuth } = result;
|
|
||||||
if (!response.ok) return null;
|
if (!response.ok) return null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
context: (await response.json()) as ZeroContext,
|
context: (await response.json()) as ZeroContext,
|
||||||
desktopAuth,
|
desktopAuth: isDesktop ? (await getDesktopAccessToken()) || undefined : undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,7 +94,7 @@ function ZeroAuthSync({ isDesktop }: { isDesktop: boolean }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isDesktop) {
|
if (isDesktop) {
|
||||||
const newToken = await getDesktopAccessToken();
|
const newToken = await getDesktopAccessToken({ forceRefresh: true });
|
||||||
if (!newToken) {
|
if (!newToken) {
|
||||||
handleUnauthorized();
|
handleUnauthorized();
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue