我在 nuxt 中实现受保护的路由,我发现最好的方法是使用中间件,所以我使用模块 cookie-universal-nuxt 来实现它。
一切都很顺利,但是当用户会话过期并且我单击任何受保护的路由时,它会将我导航到登录页面,这是我想要的,唯一的错误是当我单击浏览器上的后退按钮时(浏览器默认返回按钮),当用户会话已经过期时,它会再次显示受保护的页面。
设置 cookies 并在 localstroage 和 cookies 中。
const expireAt = Helpers.getJwtData().exp * 1000
window.localStorage.setItem('tokenExpire',expireAt);
this.$cookies.set('tokenExpire', expireAt, { path: '/'})
中间件/autheticate.js
export default function (context) {
const expiry = context.$cookies?.get('tokenExpire');
const isLoggedIn = +expiry > new Date().getTime
if (!isLoggedIn) {
Helpers.logout();
context.redirect(`/login?redirect_path=${context.route.path}`);
}
}
我也有同样的问题。 我通过将此代码添加到我的 next.config.js 文件来修复此问题
headers: () => [
{
source: '/:path*', // add your url route here
headers: [
{
key: 'Cache-Control',
value: 'no-store',
},
],
},
],