misc / bugfix: session refresh

This commit is contained in:
2025-08-28 11:15:17 -04:00
parent 315919186b
commit 1d0b310228
7 changed files with 172 additions and 153 deletions

View File

@@ -11,7 +11,7 @@ export const authFetch = async (url, options = {}, retry = true) => {
if (res.status === 401 && retry) {
// attempt refresh
try {
const refreshRes = await fetch(`${API_URL}/refresh`, {
const refreshRes = await fetch(`${API_URL}/auth/refresh`, {
method: "POST",
credentials: "include",
});
@@ -30,22 +30,23 @@ export const authFetch = async (url, options = {}, retry = true) => {
};
// Refresh token function (HttpOnly cookie flow)
export async function refreshAccessToken() {
export async function refreshAccessToken(cookieHeader) {
try {
const res = await fetch(`${API_URL}/refresh`, {
const res = await fetch(`${API_URL}/auth/refresh`, {
method: "POST",
credentials: "include", // send HttpOnly cookies
headers: {
cookie: cookieHeader || "", // forward cookies from the request
},
});
if (!res.ok) {
throw new Error("Failed to refresh token");
}
// Typically the server just updates the cookie
// It may return a new access token too, but we dont store it client-side.
return true;
// assume backend responds with new tokens in JSON
return await res.json();
} catch (err) {
console.error("Refresh token failed:", err);
return false;
return null;
}
}