这是我的代码:
export default function ({ store, redirect }) {
// If the user is not authenticated
if (!store.state.auth.auth) {
return redirect('/login')
}
}
但我的主要问题是刷新页面后我的商店是空的,因为我想在中间件中使用我的 cookie。
这是我的代码:
export default function ({ store, redirect }) {
// If the user is not authenticated
if (!store.state.auth.auth) {
return redirect('/login')
}
}
但我的主要问题是刷新页面后我的商店是空的,因为我想在中间件中使用我的 cookie。
您可以使用 cookie-universal-nuxt 在客户端和服务器端 nuxt 应用程序中设置、获取和删除 cookie https://www.npmjs.com/package/cookie-universal-nuxt
我用来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')
}
}