我不能在getServerSideProps中与HTTPS一起使用getServerSideProps()。
正常吗?我试过很多次了。
如果使用HTTPS,我会得到它的。我不能在getSession()中使用getServerSideProps
__Secure-next-auth.callback-url
__Secure-next-auth.session-token
__Host-next-auth.csrf-toke如果使用HTTP和我可以在getSession()中使用getServerSideProps是可以的
next-auth.callback-url
next-auth.session-token
next-auth.csrf-token如何将其固定在getServerSideProps中的HTTPS getServerSideProps()上?
我在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(),但是我需要get session.user.id来用prisma来获取数据库,同时prisma需要在getServerSideProps中运行
发布于 2021-08-26 16:44:40
这种行为是正常的。这些值是next-auth的内部值。当NEXTAUTH_URL以https作为前缀时,cookies将被标记为安全。你可以看到这里的行为:
在内部,next-auth将处理会话,而不考虑http或https。
要配置客户端会话,可以遵循文档中的示例:
下面是一个完整的工作示例:
首先,配置提供程序,以便在组件之间共享会话。
页/_app.js
import { Provider } from "next-auth/client"
export default function App({ Component, pageProps }) {
return (
<Provider session={pageProps.session}>
<Component {...pageProps} />
</Provider>
)
}如果您还需要在服务器端呈现期间支持身份验证,则需要。
页/index.js
import { getSession } from "next-auth/client"
export async function getServerSideProps(ctx) {
return {
props: {
session: await getSession(ctx)
}
}
}在组件内部使用next-auth提供的react钩子
import { useSession } from "next-auth/client"
export default function Component() {
const [session, loading] = useSession()
if (session) {
return <p>Signed in as {session.user.email}</p>
}
return <a href="/api/auth/signin">Sign in</a>
}在服务器端的api路由中:
import { getSession } from "next-auth/client"
export default async (req, res) => {
const session = await getSession({ req })
res.end()
}https://stackoverflow.com/questions/68941527
复制相似问题