web/src/router/guards/guard-auth.ts

34 lines
889 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { store, useUserStore } from "@/store";
import { Message } from "@arco-design/web-vue";
import { NavigationGuardWithThis } from "vue-router";
const whitelist = ["/:all(.*)*"];
const signoutlist = ["/login"];
export const authGuard: NavigationGuardWithThis<undefined> = async function (to) {
// 放在外面pinia-plugin-peristedstate 插件会失效
const userStore = useUserStore(store);
if (to.meta?.auth === false) {
return true;
}
if (whitelist.includes(to.path) || to.name === "_all") {
return true;
}
if (signoutlist.includes(to.path)) {
if (userStore.accessToken) {
Message.warning(`提示:您已登陆,如需重新请退出后再操作!`);
return false;
}
return true;
}
if (!userStore.accessToken) {
return {
path: "/login",
query: {
redirect: to.path,
},
};
}
return true;
};