我希望从服务器端(getServerSideProps)获取用户会话。现在,我正在使用passportjs中间件模式,然后在API端获取用户对象。护照上有什么功能或方法可以让我这样做吗?
发布于 2022-09-05 23:20:08
使用官方的例子:
https://github.com/vercel/next.js/tree/canary/examples/with-passport
您可以在页面中使用它从服务器端登录用户数据:
import { getLoginSession } from '../lib/auth'
import { findUser } from '../lib/user'
// ...
export async function getServerSideProps({req}) {
const session = await getLoginSession(req)
const user = (session && (await findUser(session))) ?? null
console.log(user)
return {
props: {
user,
}
}
}
https://stackoverflow.com/questions/67659802
复制相似问题