diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 6f10319..f3b9e6b 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -21,7 +21,11 @@ const queryClient = new QueryClient({ }) function ProtectedRoute({ children }: { children: React.ReactNode }) { - const { isAuthenticated } = useAuthStore() + const { isAuthenticated, isLoading } = useAuthStore() + + if (isLoading) { + return null + } if (!isAuthenticated) { return diff --git a/frontend/src/store/auth.ts b/frontend/src/store/auth.ts index cb65d4d..1da616b 100644 --- a/frontend/src/store/auth.ts +++ b/frontend/src/store/auth.ts @@ -15,7 +15,7 @@ interface AuthState { export const useAuthStore = create((set) => ({ isAuthenticated: false, - isLoading: false, + isLoading: true, error: null, showAuthModal: false, @@ -48,16 +48,17 @@ export const useAuthStore = create((set) => ({ checkAuth: async () => { const token = localStorage.getItem('adminToken') if (!token) { - set({ isAuthenticated: false }) + set({ isAuthenticated: false, isLoading: false }) return } + set({ isLoading: true }) try { await apiClient.checkAuth() - set({ isAuthenticated: true }) + set({ isAuthenticated: true, isLoading: false }) } catch (error) { localStorage.removeItem('adminToken') - set({ isAuthenticated: false }) + set({ isAuthenticated: false, isLoading: false }) } }, diff --git a/src/index.ts b/src/index.ts index 19aac6c..391be95 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,9 @@ -import { Context, Hono, Next } from 'hono' +import { Hono } from 'hono' import { cors } from 'hono/cors'; import { api as providerApi } from './providers' import { api as adminApi } from './admin' import { fromHono } from 'chanfana'; -const API_PATHS = [ - "/api", - "/v1", -]; - const app = new Hono() const openapi = fromHono(app, { schema: { @@ -24,18 +19,6 @@ const openapi = fromHono(app, { // cors openapi.use('/*', cors()); -// global middlewares -app.use('/*', async (c, next) => { - // check if the request is for static files - if (c.env.ASSETS && !API_PATHS.some(path => c.req.path.startsWith(path))) { - const url = new URL(c.req.raw.url); - if (!url.pathname.includes('.')) { - url.pathname = "" - } - return c.env.ASSETS.fetch(url); - } - await next() -}); // global error handler openapi.onError((err, c) => { console.error(err) diff --git a/wrangler.toml.template b/wrangler.toml.template index 8870f0e..e9bd93c 100644 --- a/wrangler.toml.template +++ b/wrangler.toml.template @@ -9,9 +9,10 @@ routes = [ ADMIN_TOKEN = "your-secure-admin-token-here" [assets] -directory = "public" +directory = "./public" +not_found_handling = "single-page-application" binding = "ASSETS" -run_worker_first = true +run_worker_first = ["/api/*", "/v1/*"] [[d1_databases]] binding = "DB"