所以基本上我使用 getServerSideProps 来调用一些 API。当我在 getServerSideProps() 中调用 getSession 时,我得到一个有效的对象。
export async function getServerSideProps({ req }) {
const session = await getSession({ req }); // works
Run Code Online (Sandbox Code Playgroud)
但是当我在 getServerSideProps() 函数中调用的 API 中调用它时,我得到 null。
import { getSession } from "next-auth/react";
export default async (req, res) => {
const { db } = await connectToDatabase();
const session = await getSession({ req }); // returns null
Run Code Online (Sandbox Code Playgroud)
这是 NextAuth 文档供参考: