From e2494135d6c8f10af998eb644c44c38fd444ea12 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Sun, 14 Sep 2025 21:30:12 +0200 Subject: [PATCH] fix: add redirect to login for unauthorized access in FeedPage --- thoughts-frontend/app/page.tsx | 9 +++++++-- thoughts-frontend/lib/api.ts | 1 - 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/thoughts-frontend/app/page.tsx b/thoughts-frontend/app/page.tsx index 2f0e2fc..3964e24 100644 --- a/thoughts-frontend/app/page.tsx +++ b/thoughts-frontend/app/page.tsx @@ -23,6 +23,7 @@ import { PaginationNext, PaginationPrevious, } from "@/components/ui/pagination"; +import { redirect } from "next/navigation"; export default async function Home({ searchParams, @@ -48,11 +49,15 @@ async function FeedPage({ const page = parseInt(searchParams.page ?? "1", 10); const [feedData, me] = await Promise.all([ - getFeed(token, page), + getFeed(token, page).catch(() => null), getMe(token).catch(() => null) as Promise, ]); - const { items: allThoughts, totalPages } = feedData; + if (!feedData || !me) { + redirect("/login"); + } + + const { items: allThoughts, totalPages } = feedData!; const thoughtThreads = buildThoughtThreads(allThoughts); const authors = [...new Set(allThoughts.map((t) => t.authorUsername))]; diff --git a/thoughts-frontend/lib/api.ts b/thoughts-frontend/lib/api.ts index 33c4ff2..8fbe4a8 100644 --- a/thoughts-frontend/lib/api.ts +++ b/thoughts-frontend/lib/api.ts @@ -176,7 +176,6 @@ export const loginUser = (data: z.infer) => token ); -// --- User API Functions --- export const getUserProfile = (username: string, token: string | null) => apiFetch(`/users/${username}`, {}, UserSchema, token);