出于某种原因,getServerSession可以在POST路由上工作,但不能在GET路由上工作.
import { getServerSession } from "next-auth"
import { authOptions } from "../../auth/[...nextauth]/route"
import { NextResponse } from "next/server"
export async function GET(){
const session = await getServerSession(authOptions)
console.log("Session:", session)
console.log("UserID:",session?.user.id)
return NextResponse.json({posts: []}, {status: 200})
}
import { getServerSession } from "next-auth"
import { NextRequest, NextResponse } from "next/server"
import { authOptions } from "../../auth/[...nextauth]/route"
export async function POST(req: NextRequest){
const session = await getServerSession(authOptions)
if(!session) return NextResponse.json({message: "You don't have persmision!"}, {status: 401})
//other code...
}
试图模仿POST路由,结果仍然是一样的.
编辑: 它可以在/api上运行,但不能在/api/post/*上运行.出于某种原因,这会不会是某种路由问题?
编辑2:在postman 上不起作用,但在通过浏览器调用API时起作用...?