main layout

This commit is contained in:
Julian Krauser 2024-08-23 14:42:32 +02:00
parent 62990170de
commit f1e6e8b8d3
38 changed files with 1353 additions and 20 deletions

View file

@ -0,0 +1,9 @@
import { useAccountStore } from "@/stores/account";
export async function loadAccountData(to: any, from: any, next: any) {
const account = useAccountStore();
account.fetchAccountContests();
account.fetchAccountInvites();
account.fetchAccountLicense();
next();
}

41
src/router/authGuards.ts Normal file
View file

@ -0,0 +1,41 @@
import NProgress from "nprogress";
import { useAuthStore } from "@/stores/auth";
import { useAccountStore } from "@/stores/account";
import { jwtDecode, type JwtPayload } from "jwt-decode";
type Payload = JwtPayload & { userId: number; username: string; firstname: string; lastname: string; mail: string };
export async function isAuthenticated(to: any, from: any, next: any) {
const auth = useAuthStore();
NProgress.start();
if (auth.authCheck && localStorage.getItem("access_token")) {
NProgress.done();
next();
return;
}
await isAuthenticatedPromise()
.then(async (result: any) => {
NProgress.done();
next();
})
.catch((err: Error) => {
NProgress.done();
next({ name: "login" });
});
}
export async function isAuthenticatedPromise(): Promise<Payload> {
return new Promise<Payload>((resolve, reject) => {
const auth = useAuthStore();
const account = useAccountStore();
let decoded = jwtDecode<Payload>(localStorage.getItem("accessToken") ?? "");
auth.setSuccess();
if (typeof decoded == "string" || !decoded) {
reject("jwt failed");
}
var { firstname, lastname, mail, username } = decoded;
account.setAccountData(firstname, lastname, mail, username);
resolve(decoded);
});
}

View file

@ -1,13 +1,31 @@
import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import { createRouter, createWebHistory, createWebHashHistory } from "vue-router";
import Login from "../views/Login.vue";
import { isAuthenticated } from "./authGuards";
import { loadAccountData } from "./accountGuard";
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: "/",
name: "home",
component: HomeView,
redirect: { name: "admin" },
},
{
path: "/login",
name: "login",
component: Login,
},
{
path: "/admin",
name: "admin",
component: () => import("../views/admin/View.vue"),
beforeEnter: [isAuthenticated],
},
{
path: "/:pathMatch(.*)*",
name: "404",
component: () => import("../views/notFound.vue"),
},
],
});