node.js - getSession next-auth 返回 null
问题描述
我正在学习 NextAuth,我只是想在 API 中获取当前会话。从文档中看起来很容易,但它一直给我null。如果我从客户端使用 useSession,我会得到当前会话。我究竟做错了什么?
import { getSession } from 'next-auth/react';
const handler = nc().use(Cors());
handler.post(async (req, res) => {
if (req.method === 'POST') {
const session = await getSession({ req });
if (session) {
console.log('Session', JSON.stringify(session, null, 2));
} else {
console.log('Not authenticated.');
res.status(401);
}
res.end();
解决方案
推荐阅读
- javascript - 未捕获的引用错误:未定义变量
- jquery - 我正在使用 laravel 5.4 并使用 ajax 提交
- javascript - 动态创建的 html 来获取图像 - 我想在提交之前显示检索到的图像
- datetime - 如何将 URL 中的 DateTime 值从 Postman 传递到 ASP.Net Core MVC 控制器
- python - 如何在 Python 中导入 OpenSSL 以获取证书的序列号
- java - Akka 演员正在停止
- ios - 代码签名错误 命令/usr/bin/codedesign 失败,退出代码为 1
- python - 熊猫索引日期时间格式未提供所需的输出 Python3
- javascript - jquery 不适用于 diango
- css - 如何仅为角度6中的特定组件设置背景图像