mirror of https://github.com/veypi/OneAuth.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
|
|
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('vb', '/'); // Relative path
|
|
console.log(vbase)
|
|
$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('/login?redirect=' + encodeURIComponent(to.fullPath));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!vbase.user) {
|
|
try {
|
|
await vbase.fetchUser();
|
|
} catch (e) {
|
|
vbase.logout('/login?redirect=' + encodeURIComponent(to.fullPath));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Role Check
|
|
if (roles && roles.length > 0) {
|
|
const hasRole = roles.some(role => vbase.hasRole(role));
|
|
console.log(roles, hasRole, vbase.user)
|
|
if (!hasRole) {
|
|
// $env.$router.push('/403');
|
|
// return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
next();
|
|
};
|
|
}
|