0

这是我的代码:

export default function ({ store, redirect }) {
  // If the user is not authenticated
  if (!store.state.auth.auth) {
    return redirect('/login')
  }
}

但我的主要问题是刷新页面后我的商店是空的,因为我想在中间件中使用我的 cookie。

4

2 回答 2

1

您可以使用 cookie-universal-nuxt 在客户端和服务器端 nuxt 应用程序中设置、获取和删除 cookie https://www.npmjs.com/package/cookie-universal-nuxt

于 2021-01-13T14:29:37.017 回答
0

我用来localStorage解决这个问题。

设置登录用户凭据的示例代码如下:

localStorage.setItem('CURRENT_USER', 'YOUR_LOGIN_USER_CREDENTIALS');

中间件代码示例如下:

export default function ({ store, redirect }) {
    // If the user is not authenticated
    const curUser = localStorage.getItem('CURRENT_USER');
    if (!curUser) {
        return redirect('/login')
    }
}
于 2021-01-12T15:31:25.187 回答