import router from "./router";
|
import { Message } from "element-ui";
|
import Cookies from "js-cookie";
|
import NProgress from "nprogress"; // progress bar
|
import "nprogress/nprogress.css"; // progress bar style
|
|
NProgress.configure({ showSpinner: false }); // NProgress Configuration
|
const whiteList = ["/login"]; // no redirect whitelist
|
router.beforeEach(async (to, from, next) => {
|
// determine whether the user has logged in
|
const username = Cookies.get("uname");
|
const uid = Cookies.get("uid");
|
|
// TODO
|
const isAdmin = uid && uid <= 1000;
|
|
if (username) {
|
if (to.path === "/login") {
|
// if is logged in, redirect to the home page
|
next({ path: "/" });
|
NProgress.done();
|
} else if (to.path.indexOf("/system/") > -1 ) {
|
if (isAdmin) {
|
next();
|
} else {
|
next({ path: "/" });
|
}
|
} else {
|
next();
|
}
|
} else {
|
/* has no token*/
|
if (whiteList.indexOf(to.path) !== -1) {
|
next();
|
} else {
|
next(`/login?redirect=${to.path}`);
|
NProgress.done();
|
}
|
}
|
});
|
|
router.afterEach(() => {
|
// finish progress bar
|
NProgress.done();
|
});
|