在API路由中,确保getSession返回的不是null。下面是一个示例代码:
import { getSession } from 'next-auth/client'
export default async function handler(req, res) {
const session = await getSession({ req })
if (!session) {
res.status(400).json({
error: 'Unauthorized',
message: 'Please sign in to get access.'
})
return
}
// ... 继续处理 API 请求
}
在上面的示例中,getSession 返回 null 时,将返回一个未授权的错误响应。确保在调用getSession之前用户已经进行了身份验证。
上一篇:API路由未找到