Skip to content

Commit 471c192

Browse files
committed
fix: auth redirect
1 parent ae66792 commit 471c192

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

.github/workflows/push-image.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Push Docker Image
1+
name: Docker Image
22

33
on:
44
push:

compose/compose.dev.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ services:
9595
environment:
9696
- DATABASE_URL=postgresql://${POSTGRES_USER:-pypsa}:${POSTGRES_PASSWORD:-devpassword}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/${POSTGRES_DB:-pypsa_dev}
9797
- REDIS_URL=redis://redis:6379/0
98-
- DEBUG=true
9998
volumes:
10099
- ../src:/app/src
101100
- ../data:/data

frontend/app/src/lib/api/client.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,19 @@ async function request(endpoint, options = {}, cancellationKey = null) {
4343
const response = await fetch(url, config);
4444

4545
if (!response.ok) {
46-
// Handle authentication errors
46+
const error = await response.json().catch(() => ({ detail: response.statusText }));
47+
const err = new Error(error.detail || `HTTP ${response.status}: ${response.statusText}`);
48+
err.status = response.status;
49+
50+
// Handle authentication errors - but only redirect for 401, not 400
51+
// 400 means auth is disabled, 401 means auth is enabled but user not logged in
4752
if (response.status === 401 && !endpoint.includes('/auth/')) {
4853
// Redirect to login page if not authenticated (except for auth endpoints)
4954
if (typeof window !== 'undefined' && !window.location.pathname.startsWith('/login')) {
5055
window.location.href = '/login';
5156
}
5257
}
5358

54-
const error = await response.json().catch(() => ({ detail: response.statusText }));
55-
const err = new Error(error.detail || `HTTP ${response.status}: ${response.statusText}`);
56-
err.status = response.status;
5759
throw err;
5860
}
5961

frontend/app/src/lib/stores/auth.svelte.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class AuthStore {
99
user = $state(null);
1010
loading = $state(true);
1111
error = $state(null);
12+
authEnabled = $state(null); // null = unknown, true = enabled, false = disabled
1213

1314
/**
1415
* Initialize auth state by fetching current user
@@ -21,13 +22,21 @@ class AuthStore {
2122
try {
2223
const response = await auth.me();
2324
this.user = response;
25+
this.authEnabled = true; // Auth is enabled and user is logged in
2426
} catch (err) {
25-
// Not logged in or auth is disabled
26-
this.user = null;
27-
// Don't set error for 401/400, these are expected when not logged in
28-
if (err.status && (err.status === 401 || err.status === 400)) {
27+
// Check if auth is disabled (400 error)
28+
if (err.status === 400) {
29+
// Auth is disabled - no login required
30+
this.authEnabled = false;
31+
this.user = null;
32+
this.error = null;
33+
} else if (err.status === 401) {
34+
// Auth is enabled but user is not logged in
35+
this.authEnabled = true;
36+
this.user = null;
2937
this.error = null;
3038
} else {
39+
// Other error
3140
console.error('Failed to fetch user:', err);
3241
this.error = err.message;
3342
}

frontend/app/src/routes/+layout.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
// Note: The API client's automatic 401 redirect skips /auth/ endpoints
5858
// to prevent redirect loops, so we handle the redirect manually here
5959
const currentPath = $page.url.pathname;
60-
if (!authStore.loading && !authStore.isAuthenticated) {
60+
if (!authStore.loading && authStore.authEnabled && !authStore.isAuthenticated) {
6161
if (currentPath !== '/login') {
6262
goto('/login');
6363
}

0 commit comments

Comments
 (0)