import VBase from './vbase.js' export default async ($env) => { // Load i18n try { const langs = await (await fetch('/langs.json')).json() $env.$i18n.load(langs) } catch (e) { console.error('Failed to load langs.json', e) } // Initialize VBase Service const vbase = new VBase(''); // Relative path $env.$vbase = vbase; // Wrap Axios vbase.wrapAxios($env.$axios); // Router Guard $env.$router.beforeEnter = async (to, from, next) => { const isAuth = to.meta && to.meta.auth; const isGuest = to.meta && to.meta.guest; const roles = to.meta && to.meta.roles; // Array of required roles if (isAuth) { if (vbase.isExpired()) { try { await vbase.refresh(); } catch (e) { vbase.logout(to.fullPath); return false; } } if (!vbase.user) { try { await vbase.fetchUser(); } catch (e) { vbase.logout(to.fullPath); return false; } } // Role Check if (roles && roles.length > 0) { const hasRole = roles.some(role => vbase.hasRole(role)); if (!hasRole) { $env.$router.push('/403'); return false; } } } else if (isGuest) { if (!vbase.isExpired()) { next('/'); return false; } } next(); }; }