From 85c14002c6a2de51602f2279e0eca5b502de0dc7 Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Wed, 29 Jan 2025 22:46:13 +0100 Subject: [PATCH] auth: navigate to auth page when API calls are not authenticated (#269) --- apps/readest-app/src/app/auth/page.tsx | 3 ++- apps/readest-app/src/hooks/useSync.ts | 6 ++++++ apps/readest-app/src/utils/nav.ts | 5 ++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/readest-app/src/app/auth/page.tsx b/apps/readest-app/src/app/auth/page.tsx index 29f3a371..7a533425 100644 --- a/apps/readest-app/src/app/auth/page.tsx +++ b/apps/readest-app/src/app/auth/page.tsx @@ -167,7 +167,8 @@ export default function AuthPage() { const { data: subscription } = supabase.auth.onAuthStateChange((_event, session) => { if (session?.access_token && session.user) { login(session.access_token, session.user); - router.push('/library'); + const redirectTo = new URLSearchParams(window.location.search).get('redirect'); + router.push(redirectTo ?? '/library'); } }); diff --git a/apps/readest-app/src/hooks/useSync.ts b/apps/readest-app/src/hooks/useSync.ts index 194ffaf4..a09dccc5 100644 --- a/apps/readest-app/src/hooks/useSync.ts +++ b/apps/readest-app/src/hooks/useSync.ts @@ -1,4 +1,5 @@ import { useEffect, useRef, useState } from 'react'; +import { useRouter } from 'next/navigation'; import { useSyncContext } from '@/context/SyncContext'; import { SyncData, SyncOp, SyncResult, SyncType } from '@/libs/sync'; import { useSettingsStore } from '@/store/settingsStore'; @@ -8,6 +9,7 @@ import { transformBookNoteFromDB } from '@/utils/transform'; import { transformBookFromDB } from '@/utils/transform'; import { DBBook, DBBookConfig, DBBookNote } from '@/types/records'; import { Book, BookConfig, BookDataRecord, BookNote } from '@/types/book'; +import { navigateToLogin } from '@/utils/nav'; const transformsFromDB = { books: transformBookFromDB, @@ -33,6 +35,7 @@ const computeMaxTimestamp = (records: BookDataRecord[]): number => { const SEVEN_DAYS_IN_MS = 7 * 24 * 60 * 60 * 1000; export function useSync(bookKey?: string) { + const router = useRouter(); const { settings, setSettings } = useSettingsStore(); const { getConfig, setConfig } = useBookDataStore(); const config = bookKey ? getConfig(bookKey) : null; @@ -118,6 +121,9 @@ export function useSync(bookKey?: string) { } catch (err: unknown) { console.error(err); if (err instanceof Error) { + if (err.message.includes('Not authenticated')) { + navigateToLogin(router); + } setSyncError(err.message || `Error pulling ${type}`); } else { setSyncError(`Error pulling ${type}`); diff --git a/apps/readest-app/src/utils/nav.ts b/apps/readest-app/src/utils/nav.ts index d0a08c14..5ab1c277 100644 --- a/apps/readest-app/src/utils/nav.ts +++ b/apps/readest-app/src/utils/nav.ts @@ -19,7 +19,10 @@ export const navigateToReader = ( }; export const navigateToLogin = (router: ReturnType) => { - router.push('/auth'); + const pathname = window.location.pathname; + const search = window.location.search; + const currentPath = pathname !== '/auth' ? pathname + search : '/'; + router.push(`/auth?redirect=${encodeURIComponent(currentPath)}`); }; export const navigateToLibrary = (router: ReturnType) => {