80 lines
2.0 KiB
TypeScript
80 lines
2.0 KiB
TypeScript
import { createApp } from "vue";
|
|
import App from "./App.vue";
|
|
import "./index.css";
|
|
import router from "./router";
|
|
import { createPinia } from "pinia";
|
|
import { useBudgetsStore } from "./stores/budget";
|
|
import { useAccountStore } from "./stores/budget-account";
|
|
import PiniaLogger from "./pinia-logger";
|
|
import { useSessionStore } from "./stores/session";
|
|
|
|
const app = createApp(App);
|
|
app.use(router);
|
|
|
|
const pinia = createPinia();
|
|
pinia.use(
|
|
PiniaLogger({
|
|
expanded: false,
|
|
showDuration: true,
|
|
})
|
|
);
|
|
app.use(pinia);
|
|
app.mount("#app");
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
const budgetStore = useBudgetsStore();
|
|
await budgetStore.SetCurrentBudget(<string>to.params.budgetid);
|
|
|
|
const accountStore = useAccountStore();
|
|
await accountStore.SetCurrentAccount(
|
|
<string>to.params.budgetid,
|
|
<string>to.params.accountid
|
|
);
|
|
next();
|
|
});
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
const sessionStore = useSessionStore();
|
|
const token = sessionStore.Session?.Token;
|
|
let loggedIn = false;
|
|
|
|
if (token != null) {
|
|
const jwt = parseJwt(token);
|
|
if (jwt.exp > Date.now() / 1000) loggedIn = true;
|
|
}
|
|
|
|
if (to.matched.some((record) => record.meta.requiresAuth)) {
|
|
if (!loggedIn) {
|
|
next({ path: "/login" });
|
|
} else {
|
|
next();
|
|
}
|
|
} else if (to.matched.some((record) => record.meta.hideForAuth)) {
|
|
if (loggedIn) {
|
|
next({ path: "/dashboard" });
|
|
} else {
|
|
next();
|
|
}
|
|
} else {
|
|
next();
|
|
}
|
|
});
|
|
|
|
function parseJwt(token: string) {
|
|
var base64Url = token.split(".")[1];
|
|
var base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
|
|
var jsonPayload = decodeURIComponent(
|
|
atob(base64)
|
|
.split("")
|
|
.map(function (c) {
|
|
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
|
|
})
|
|
.join("")
|
|
);
|
|
|
|
return JSON.parse(jsonPayload);
|
|
}
|
|
|
|
1646426130;
|
|
1646512855755;
|