我不能在getServerSideProps中使用getSession()和HTTPS。
正常吗?我试过很多次。
如果使用HTTPS,我会得到它。我无法在getServerSideProps中使用getSession()
__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),
},
};
}
备注:
1.我在.env
中设置了NEXTAUTH_URL
1.我知道我可以在getInitialProps
中获得getSession(),但我需要获得session.user.id来使用prisma
获取数据库,同时prisma
需要在getServerSideProps中运行
2条答案
按热度按时间zi8p0yeb1#
这种行为是正常的。这些值是
next-auth
的内部值。当NEXTAUTH_URL
前缀为https
时,cookie将被标记为安全。你可以看到这里的行为:https://github.com/nextauthjs/next-auth/blob/543f812eb32448044d2aa725b623ca1dedbb68a3/src/lib/jwt.js#L115
在内部,
next-auth
将处理会话,而不考虑http
或https
。要配置客户端会话,您可以按照文档中的示例操作:
这里有一个完整的工作示例:
首先配置提供程序,以便在组件之间共享会话。
pages/_app.js
如果在服务器端呈现期间也需要支持身份验证,则需要。
pages/index.js
在组件内部使用
next-auth
提供的react钩子在服务器端的API路由中:
r1zhe5dt2#
使用“next 13.3.4”,“next-auth 4.22.1”“react 18.2.0”,以下内容似乎可以正常工作: