From dc6ff1225f2c352afa57ed7fabe826321e181810 Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Sun, 3 Aug 2025 12:57:29 +0200 Subject: [PATCH] Error api call if not logged in --- app/api/profile/route.ts | 7 +++++-- app/profile/page.tsx | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/api/profile/route.ts b/app/api/profile/route.ts index 5b3ae8e8..8552f140 100644 --- a/app/api/profile/route.ts +++ b/app/api/profile/route.ts @@ -9,8 +9,11 @@ export async function GET() { const session = await getSession(); console.log(`Session: ${session?.user?.name}`); - if (!session?.user?.email) - redirect('/login'); + if (!session?.user?.id) + return new NextResponse(JSON.stringify({ error: "User not found" }), { + status: 404, + headers: { "Content-Type": "application/json" }, + }); const id = session.user.id; diff --git a/app/profile/page.tsx b/app/profile/page.tsx index e8afda12..4c371ffd 100644 --- a/app/profile/page.tsx +++ b/app/profile/page.tsx @@ -1,11 +1,25 @@ 'use client'; import Link from 'next/link'; -import {usePathname} from "next/navigation"; +import {usePathname, useRouter} from "next/navigation"; import {getProfile} from "@/lib/client/profile"; +import {useEffect} from "react"; +import {useSession} from "next-auth/react"; export default function ProfilePage() { const pathname = usePathname(); // Get the current route + const router = useRouter(); + const {data: session} = useSession(); + + useEffect(() => { + async function asyncRun() { + if (!session?.user?.id) + router.push('/login'); + } + + asyncRun(); + }, []); + try { const header = (