LoongPanel-Asp/web/middleware/auth.ts

36 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

2024-06-22 10:54:02 +08:00
import {defineNuxtRouteMiddleware} from "#app";
import {useToast} from 'vue-toastification'
2024-07-02 16:08:07 +08:00
2024-06-22 10:54:02 +08:00
export default defineNuxtRouteMiddleware(async (to, from) => {
const toast = useToast();
const runtimeConfig = useRuntimeConfig();
const token = useCookie('token').value;
if (!token) {
toast.error('未登录', {timeout: 3000})
return navigateTo("/SignIn");
}
//如果导航到/
if (to.path === '/' && token) {
return navigateTo("/Home");
}
// 获取当前导航路径
const currentPath = to.path;
try {
2024-07-02 16:08:07 +08:00
await $fetch('/Api/Rote/RoteVerify', {
2024-06-22 10:54:02 +08:00
method: 'GET',
headers: {
'Authorization': 'Bearer ' + token
},
baseURL: runtimeConfig.public.baseUrl,
params: {'path': currentPath}
});
2024-07-02 16:08:07 +08:00
// 如果验证成功,继续导航
return true;
} catch (err) {
toast.error('权限不足', {timeout: 3000});
return navigateTo("/error/403");
2024-06-22 10:54:02 +08:00
}
});