Skip to content

The only tool you need to know what is happening and how to fix it.

fix: auth cleanup + fix for multiprojects

narrow-adventurea6d3d1b20d09
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 05a76276..de2a7784 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -1,4 +1,6 @@
import { authState } from './state/auth.svelte';
+import { projectsState } from './state/projects.svelte';
+import { gotoHref } from './utils/navigation';
import { toast } from 'svelte-sonner';
const BASE_URL = '/api';
@@ -8,76 +10,106 @@ interface RequestOptions {
skipProjectId?: boolean;
}
-async function request(method: string, endpoint: string, data?: unknown, options?: RequestOptions) {
- const currentToken = authState.token;
- const headers: Record<string, string> = {
- 'Content-Type': 'application/json'
- };
-
- if (currentToken) {
- headers['Authorization'] = `Bearer ${currentToken}`;
- }
-
- const config: RequestInit = {
- method,
- headers
- };
+let recovering = false;
+let staleProjectNotified = '';
- if (data) {
- config.body = JSON.stringify(data);
+async function request(method: string, endpoint: string, data?: unknown, options?: RequestOptions) {
+ const token = authState.token;
+ const headers: Record<string, string> = { 'Content-Type': 'application/json' };
+ if (token) {
+ headers['Authorization'] = `Bearer ${token}`;
}
- // Add projectId as query parameter if provided
let url = `${BASE_URL}${endpoint}`;
if (options?.projectId && !options?.skipProjectId) {
- const separator = endpoint.includes('?') ? '&' : '?';
- url = `${url}${separator}projectId=${options.projectId}`;
+ url += `${endpoint.includes('?') ? '&' : '?'}projectId=${options.projectId}`;
}
- const response = await fetch(url, config);
+ const response = await fetch(url, {
+ method,
+ headers,
+ body: data ? JSON.stringify(data) : undefined
+ });
+
+ if (authState.token !== token) {
+ throw Object.assign(new Error('Session changed'), { status: 0 });
+ }
if (response.status === 401) {
authState.logout();
- window.location.href = '/login';
- throw new Error('Unauthorized');
+ const current = window.location.pathname + window.location.search;
+ gotoHref(
+ current === '/' || current.startsWith('/login')
+ ? '/login'
+ : `/login?returnTo=${encodeURIComponent(current)}`,
+ { replaceState: true }
+ );
+ throw Object.assign(new Error('Unauthorized'), { status: 401 });
}
if (response.status === 403) {
- if (method !== 'GET') {
- const body = await response.json().catch(() => ({}));
- const message = body.error || "You don't have permission to perform this action";
- toast.error(message);
- const error = new Error(message) as Error & { status: number; body?: unknown };
- error.status = 403;
- error.body = body;
- throw error;
- }
- const currentPath = window.location.pathname;
- if (currentPath === '/' || currentPath === '') {
- authState.logout();
- window.location.href = '/login';
- } else {
- toast.warning("You don't have permission to access that feature");
- window.location.href = '/';
+ const body = await response.json().catch(() => ({}));
+ const message =
+ body.error ||
+ (method === 'GET' ? 'Forbidden' : "You don't have permission to perform this action");
+
+ if (!recovering) {
+ recovering = true;
+ try {
+ const bundle = await request('GET', '/me/login-bundle').catch(() => null);
+ if (bundle) {
+ authState.setOrganizations(bundle.organizations ?? []);
+ projectsState.setProjects(bundle.projects ?? []);
+ }
+
+ if (authState.token === token) {
+ const requestedProjectId = new URL(url, window.location.origin).searchParams.get(
+ 'projectId'
+ );
+ const staleProject =
+ bundle &&
+ requestedProjectId &&
+ !projectsState.projects.some((project) => project.id === requestedProjectId);
+
+ if (staleProject) {
+ if (staleProjectNotified !== requestedProjectId) {
+ staleProjectNotified = requestedProjectId;
+ toast.warning('That project is no longer available');
+ }
+ const current = new URL(window.location.href);
+ if (
+ current.searchParams.get('projectId') === requestedProjectId &&
+ projectsState.currentProjectId
+ ) {
+ current.searchParams.set('projectId', projectsState.currentProjectId);
+ await gotoHref(current.pathname + current.search, { replaceState: true });
+ }
+ } else if (method !== 'GET') {
+ toast.error(message);
+ } else {
+ toast.warning("You don't have permission to access that feature");
+ if (window.location.pathname !== '/') {
+ await gotoHref('/', { replaceState: true });
+ }
+ }
+ }
+ } finally {
+ recovering = false;
+ }
}
- throw new Error('Forbidden');
+
+ throw Object.assign(new Error(message), { status: 403, body });
}
if (response.status === 422) {
const body = await response.json().catch(() => ({}));
- const error = new Error(body.error || 'Validation failed') as Error & {
- status: number;
- body?: unknown;
- };
- error.status = 422;
- error.body = body;
- throw error;
+ throw Object.assign(new Error(body.error || 'Validation failed'), { status: 422, body });
}
if (!response.ok) {
- const error = new Error(`API Error: ${response.statusText}`) as Error & { status: number };
- error.status = response.status;
- throw error;
+ throw Object.assign(new Error(`API Error: ${response.statusText}`), {
+ status: response.status
+ });
}
return response.json();
diff --git a/frontend/src/lib/state/auth.svelte.ts b/frontend/src/lib/state/auth.svelte.ts
index ddeb8025..6bb95dab 100644
--- a/frontend/src/lib/state/auth.svelte.ts
+++ b/frontend/src/lib/state/auth.svelte.ts
@@ -7,8 +7,19 @@ export interface UserOrganizationResponse {
timezone: string;
}
+function readStoredToken(): string | null {
+ const token = localStorage.getItem('AUTH_TOKEN');
+ if (!token) return null;
+ try {
+ const { exp } = JSON.parse(atob(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')));
+ return exp * 1000 <= Date.now() ? null : token;
+ } catch {
+ return token;
+ }
+}
+
class AuthState {
- token = $state<string | null>(localStorage.getItem('AUTH_TOKEN'));
+ token = $state<string | null>(readStoredToken());
organizations = $state<UserOrganizationResponse[]>(
JSON.parse(localStorage.getItem('USER_ORGANIZATIONS') || '[]')
);