Reference Error:next.js应用程序中未定义窗口,我需要持续存储一个变量

发布于 2025-02-10 13:17:21 字数 1263 浏览 0 评论 0 原文

这是我的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 | 

如何在客户端的浏览器中持续存储有效载荷?如果您能想到的话,我愿意尝试其他方法,但是我需要客户的浏览器保留有效载荷以备将来访问。

This is my getServerSideProps code:

//@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);
}

I

This is the error I get in my console

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 | 

How can I store payload persistently in the client's browser? I'd be willing to try some other method if you can think of one but I need the client's browser to retain payload for future access.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

束缚m 2025-02-17 13:17:21

您可以将数据持续到cookie中。您可以从

res.header('set-cookie: key=value; max-age=2592000');

。或使用 cookies-next )之类的库。

import { setCookies, getCookie  } from 'cookies-next';

setCookies('key', 'value', options);
getCookie('key', options); // => 'value'

You can persist the data in the cookies. You can do so from scratch with

res.header('set-cookie: key=value; max-age=2592000');

Add retrieve cookies from document.cookie. Or use a library like cookies-next.

import { setCookies, getCookie  } from 'cookies-next';

setCookies('key', 'value', options);
getCookie('key', options); // => 'value'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文