How to identify a user ID when a server receives requests?

Are this steps correct? or there is the method? 1. Receive requests 2. Get a session token in the cookie 3. SELECT userId FROM session WHERE token = "token" environment: Next.js App router Route Handlers prisma
Solution:
I solved that!! app/api/record/route.ts ``` import { headers } from "next/headers";...
Jump to solution
1 Reply
Solution
カリカリ
カリカリ3mo ago
I solved that!! app/api/record/route.ts
import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";

export async function GET(req: Request) {
const session = await auth.api.getSession({
headers: await headers(),
});

console.log("Session:", session);
console.log("ID:", session?.user.id);

if (!session) {
return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
}
return NextResponse.json({ message: "Success", user: session.user });
}
import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";

export async function GET(req: Request) {
const session = await auth.api.getSession({
headers: await headers(),
});

console.log("Session:", session);
console.log("ID:", session?.user.id);

if (!session) {
return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
}
return NextResponse.json({ message: "Success", user: session.user });
}

Did you find this page helpful?