我正在使用nuxt2和vuex-persistedstate保持状态,并且我将防止未经登录即访问一个URL。 所以我用中间件检查它是否已登录。
// middleware/auth.js
import jwt from 'jsonwebtoken'
export default function({ store, route, redirect }) {
if (route.path === '/protectedurl') {
if (!store.state.auth.token) {
redirect('/protectedurl/login')
} else {
const decoded = jwt.verify(store.state.auth.token, process.env.privateKey)
if (
decoded.isLoggedIn !== true ||
Math.floor(Date.now() / 1000) > decoded.exp
) {
redirect('/protectedurl/login')
}
}
}
}
当我使用<nuxt-link>
从其他页面导航到受保护的URL时,效果很好
但是,当我刷新时,它无法读取具有持久值的商店。
而且,我能够在组件的updated()
生命周期功能中读取正确的数据。
我认为这是因为中间件是在插件之前被确认的,所以中间件首先会读取null。
有人可以帮我解决这个问题吗?