0

我有以下代码片段正在执行经过身份验证的提取。目前,如果我让应用程序运行并保持非活动状态超过 2 小时,我的身份验证令牌将过期并且服务器返回错误(我必须手动刷新页面并再次登录)。

我想要的是以下内容: 当我从 authFetch 捕获未处理的拒绝(TypeError)时,重新加载页面。因此,用户可以再次登录而不会看到错误。

这是它的代码:

useEffect(() => {
authFetch('/api/business/spaces', {
  method: 'GET',
  headers: { 'Content-Type': 'application/json' },
})
  .then((response) => response.json())
  .then((resp) => setSpaces(resp.spaces))
  .catch((error) => {
    console.log(
      'TokenExpired, can not connect to server (refreshing page...):'
    )
    console.log(error)
    window.location.href = window.location.href + ''
  })

我的问题是:当我运行前端/后端时,页面立即开始不停地刷新。好像它不断地捕捉错误。

我可以更改它,以便它仅在我捕获未处理的拒绝错误时刷新页面,而不仅仅是任何错误?

4

0 回答 0