首页 > 解决方案 > 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();

标签: node.jsreactjsnext.jsnext-auth

解决方案


推荐阅读