【发布时间】:2021-10-26 16:36:36
【问题描述】:
我无法通过 HTTPS 在 getServerSideProps 中使用 getSession()。
正常吗?我试了很多次。
如果使用 HTTPS 我会得到它。我无法在 getServerSideProps 中获取会话()
__Secure-next-auth.callback-url
__Secure-next-auth.session-token
__Host-next-auth.csrf-toke
如果使用 HTTP 并且我可以在 getServerSideProps 中使用 getSession() 就可以了
next-auth.callback-url
next-auth.session-token
next-auth.csrf-token
如何在 getServerSideProps 中的 HTTPS getSession() 上修复它?
我在 http 或 https 上运行相同的代码进行测试
如果使用 http 运行,我可以获得 props.session 如果使用 https 运行,我无法获取 props.session
import { getSession } from 'next-auth/client';
export default function Home(props) {
console.log(props.session);
return (
<div>
<h1>Server Side Rendering</h1>
</div>
);
}
export async function getServerSideProps(context) {
return {
props: {
session: await getSession(context),
},
};
}
备注:
- 我在
.env中设置了NEXTAUTH_URL - 我知道我可以在
getInitialProps中获取getSession() 但是我需要用prisma获取session.user.id来获取数据库,同时prisma需要在getServerSideProps中运行
【问题讨论】:
-
您是否配置了
NEXTAUTH_URL环境变量?从控制使用哪个协议的内存中。 next-auth.js.org/configuration/options#nextauth_url -
@razboy 当然,我试过如果设置
NEXTAUTH_URL=http://localhost:3000并使用HTTP 运行,一切正常。但是如果设置NEXTAUTH_URL=https://localhost:3000并使用 HTTPS 运行。我无法在getServerSideProps中获得任何会话 -
啊,我认为 Next.js 不会在本地运行 SSL。您可能需要在其前面运行代理或配置自定义服务器。 Http 应该可以用于本地测试。也许你可以部署在某个地方进行 https 测试。
-
只是一个演示。因为我在我的生产自定义服务器上进行测试,所以我需要使用 SSL 运行。