Reference Error:next.js应用程序中未定义窗口,我需要持续存储一个变量
这是我的getServersideProps代码:
//@ts-ignore
export async function getServerSideProps({ req, res, query }) {
const { id } = query
const cookies = Cookies(req, res)
const jwt = cookies.get('lit-auth')
if (!jwt) {
return {
props: {
authorized: false
},
}
}
const { verified, payload } = LitJsSdk.verifyJwt({ jwt })
console.log(verified)
console.log(payload)
if (
payload.baseUrl !== "http://localhost:3000"
|| payload.path !== '/protected'
|| payload.extraData !== id
) {
return {
props: {
authorized: false
},
}
}
storePersistence(payload)
return {
props: {
authorized: verified ? true : false
},
}
}
function storePersistence(payload: string) {
window.localStorage.setItem('lit_protocols_jwt', payload);
}
我
这是我在控制台中遇到的错误,
error - src/pages/protected.tsx (199:2) @ storePersistence
ReferenceError: window is not defined
197 |
198 | function storePersistence(payload: string) {
> 199 | window.localStorage.setItem('lit_protocols_jwt', payload);
| ^
200 | }
201 |
如何在客户端的浏览器中持续存储有效载荷
?如果您能想到的话,我愿意尝试其他方法,但是我需要客户的浏览器保留有效载荷
以备将来访问。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将数据持续到cookie中。您可以从
。或使用 cookies-next )之类的库。
You can persist the data in the cookies. You can do so from scratch with
Add retrieve cookies from document.cookie. Or use a library like cookies-next.