Skip to content

Commit 1440eca

Browse files
committed
fix: stop 401 interceptor redirect loop during login - use plain fetch for pre-auth calls
1 parent 9e0d0a4 commit 1440eca

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

app/auth/login/page.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,19 @@ export default function LoginPage() {
6161
let merchantName = isMockAdmin ? 'System Admin' : 'Merchant User';
6262

6363
try {
64-
// Try fetching the seeded merchant from the backend
65-
const { apiClient } = await import('@/lib/api/axios');
66-
const response = await apiClient.get(`/api/merchants/${merchantId}`);
67-
if (response.data && !response.data.error) {
68-
merchantId = response.data.id;
69-
merchantName = response.data.name;
64+
// Use plain fetch (no auth interceptors) to avoid triggering the 401→redirect chain
65+
// during the pre-auth merchant lookup. The endpoint requires a JWT we don't have yet.
66+
const apiBase = process.env.NEXT_PUBLIC_API_URL || 'https://bettapay-backend.onrender.com';
67+
const res = await fetch(`${apiBase}/api/merchants/${merchantId}`, {
68+
method: 'GET',
69+
headers: { 'Content-Type': 'application/json' },
70+
});
71+
if (res.ok) {
72+
const data = await res.json();
73+
if (data && !data.error) {
74+
merchantId = data.data?.id ?? data.id ?? merchantId;
75+
merchantName = data.data?.name ?? data.name ?? merchantName;
76+
}
7077
}
7178
} catch {
7279
console.warn('Backend unavailable, falling back to mock auth for Vercel preview.');

lib/api/axios.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ function processQueue(error: unknown, token: string | null) {
6060
}
6161

6262
function redirectToLogin() {
63+
// Don't redirect if already on an auth page — prevents infinite redirect loops
64+
if (typeof window !== 'undefined' && window.location.pathname.startsWith('/auth')) {
65+
return;
66+
}
6367
useAuthStore.getState().logout();
6468
if (typeof window !== 'undefined') {
6569
window.location.href = '/auth/login';

0 commit comments

Comments
 (0)