Error api call if not logged in

This commit is contained in:
MartinBraquet
2025-08-03 12:57:29 +02:00
parent 38c7cccf04
commit dc6ff1225f
2 changed files with 20 additions and 3 deletions

View File

@@ -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;

View File

@@ -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 = (