diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index e8971cb..d292f74 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -46,6 +46,7 @@ export default function LoginPage() { if (result.success) { // Redirect to verify page with mobile number router.push(`/verify-code?mobile=${encodeURIComponent(normalizedMobile)}`) + return // Don't set loading to false — component will unmount } else { setError(result.message || 'خطا در ارسال کد') } diff --git a/src/app/page.tsx b/src/app/page.tsx index d33df9a..38fa997 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -62,6 +62,7 @@ export default function Home() { if (result.success) { // Redirect to verify page with mobile number router.push(`/verify-code?mobile=${encodeURIComponent(normalizedMobile)}`) + return // Don't set loading to false — component will unmount } else { setError(result.message || 'خطا در ارسال کد') } diff --git a/src/app/verify-code/page.tsx b/src/app/verify-code/page.tsx index 1a16294..d0aa1c1 100644 --- a/src/app/verify-code/page.tsx +++ b/src/app/verify-code/page.tsx @@ -93,14 +93,17 @@ function VerifyCodeContent() { } else if (devicesResult.totalCount === 1) { // Single device - redirect to calendar router.push(`/calendar?deviceId=${devicesResult.items[0].id}`) + return // Don't set loading to false — component will unmount } else { // Multiple devices - redirect to device list router.push('/devices') + return // Don't set loading to false — component will unmount } } catch (deviceError) { console.error('Error fetching devices:', deviceError) // Still redirect to devices page to handle error there router.push('/devices') + return // Don't set loading to false — component will unmount } } else { setError(result.message || 'کد وارد شده نادرست است') diff --git a/src/lib/api/client.ts b/src/lib/api/client.ts index 1675e07..4712dcc 100644 --- a/src/lib/api/client.ts +++ b/src/lib/api/client.ts @@ -17,6 +17,8 @@ import type { const API_BASE = process.env.NEXT_PUBLIC_API_URL ?? 'https://ghback.nabaksoft.ir' +console.log('api-base: ', API_BASE); + async function http(url: string, init?: RequestInit): Promise { const res = await fetch(url, { ...init, headers: { 'Content-Type': 'application/json', ...(init?.headers || {}) } }) if (!res.ok) { @@ -33,7 +35,14 @@ async function http(url: string, init?: RequestInit): Promise { error.status = res.status throw error } - return res.json() as Promise + + // Check if response has content + const text = await res.text() + if (!text) { + return undefined as T // or return null as T, depending on your preference + } + + return JSON.parse(text) as T } export const api = { @@ -65,7 +74,12 @@ export const api = { updateDeviceSettings: (dto: DeviceSettingsDto) => http(`${API_BASE}/api/devicesettings`, { method: 'PUT', body: JSON.stringify(dto) }), // Authentication - sendCode: (request: SendCodeRequest) => http(`${API_BASE}/api/auth/send-code`, { method: 'POST', body: JSON.stringify(request) }), + sendCode: (request: SendCodeRequest) => { + const url = `${API_BASE}/api/auth/send-code`; + + console.log('posting to ' + url); + return http(url, { method: 'POST', body: JSON.stringify(request) }) + }, verifyCode: (request: VerifyCodeRequest) => http(`${API_BASE}/api/auth/verify-code`, { method: 'POST', body: JSON.stringify(request) }), canResend: (mobile: string) => http<{ canResend: boolean }>(`${API_BASE}/api/auth/can-resend?mobile=${encodeURIComponent(mobile)}`),