2024-08-23 14:42:32 +02:00
|
|
|
import NProgress from "nprogress";
|
|
|
|
import { useAuthStore } from "@/stores/auth";
|
|
|
|
import { useAccountStore } from "@/stores/account";
|
|
|
|
import { jwtDecode, type JwtPayload } from "jwt-decode";
|
2024-08-25 10:10:11 +02:00
|
|
|
import { refreshToken } from "../serverCom";
|
2024-08-26 13:46:54 +02:00
|
|
|
import type { PermissionObject } from "../types/permissionTypes";
|
2024-08-23 14:42:32 +02:00
|
|
|
|
2024-08-26 13:46:54 +02:00
|
|
|
export type Payload = JwtPayload & {
|
|
|
|
userId: number;
|
|
|
|
username: string;
|
|
|
|
firstname: string;
|
|
|
|
lastname: string;
|
|
|
|
mail: string;
|
|
|
|
permissions: PermissionObject;
|
|
|
|
};
|
2024-08-23 14:42:32 +02:00
|
|
|
|
|
|
|
export async function isAuthenticated(to: any, from: any, next: any) {
|
|
|
|
const auth = useAuthStore();
|
|
|
|
NProgress.start();
|
2024-08-26 13:46:54 +02:00
|
|
|
if (auth.authCheck && localStorage.getItem("access_token") && localStorage.getItem("refresh_token")) {
|
2024-08-23 14:42:32 +02:00
|
|
|
NProgress.done();
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await isAuthenticatedPromise()
|
2024-08-26 13:46:54 +02:00
|
|
|
.then(async (result: Payload) => {
|
2024-08-23 14:42:32 +02:00
|
|
|
NProgress.done();
|
|
|
|
next();
|
|
|
|
})
|
2024-08-26 13:46:54 +02:00
|
|
|
.catch((err: string) => {
|
2024-08-23 14:42:32 +02:00
|
|
|
NProgress.done();
|
2024-08-26 13:46:54 +02:00
|
|
|
next({ name: err ?? "login" });
|
2024-08-23 14:42:32 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function isAuthenticatedPromise(): Promise<Payload> {
|
2024-08-25 10:10:11 +02:00
|
|
|
return new Promise<Payload>(async (resolve, reject) => {
|
2024-08-23 14:42:32 +02:00
|
|
|
const auth = useAuthStore();
|
|
|
|
const account = useAccountStore();
|
2024-08-25 13:37:23 +02:00
|
|
|
let decoded: Payload | string = "";
|
|
|
|
try {
|
|
|
|
decoded = jwtDecode<Payload>(localStorage.getItem("accessToken") ?? "");
|
|
|
|
} catch (error) {
|
2024-08-26 13:46:54 +02:00
|
|
|
auth.setFailed();
|
|
|
|
reject("login");
|
2024-08-25 13:37:23 +02:00
|
|
|
}
|
2024-08-23 14:42:32 +02:00
|
|
|
|
|
|
|
if (typeof decoded == "string" || !decoded) {
|
2024-08-26 13:46:54 +02:00
|
|
|
auth.setFailed();
|
|
|
|
reject("login");
|
2024-08-25 13:37:23 +02:00
|
|
|
} else {
|
|
|
|
// check jwt expiry
|
|
|
|
const exp = decoded.exp ?? 0;
|
2024-08-26 13:46:54 +02:00
|
|
|
const correctedLocalTime = new Date().getTime();
|
2024-08-25 13:37:23 +02:00
|
|
|
if (exp < Math.floor(correctedLocalTime / 1000)) {
|
|
|
|
await refreshToken()
|
|
|
|
.then(() => {
|
|
|
|
console.log("fetched new token");
|
|
|
|
})
|
2024-08-26 13:46:54 +02:00
|
|
|
.catch((err: string) => {
|
|
|
|
console.log("expired");
|
|
|
|
auth.setFailed();
|
|
|
|
reject(err);
|
2024-08-25 13:37:23 +02:00
|
|
|
});
|
|
|
|
}
|
2024-08-25 10:10:11 +02:00
|
|
|
|
2024-08-26 13:46:54 +02:00
|
|
|
var { firstname, lastname, mail, username, permissions } = decoded;
|
|
|
|
|
|
|
|
if (Object.keys(permissions).length === 0) {
|
|
|
|
auth.setFailed();
|
|
|
|
reject("nopermissions");
|
|
|
|
}
|
|
|
|
|
|
|
|
auth.setSuccess();
|
|
|
|
account.setAccountData(firstname, lastname, mail, username, permissions);
|
2024-08-25 13:37:23 +02:00
|
|
|
resolve(decoded);
|
2024-08-25 10:10:11 +02:00
|
|
|
}
|
2024-08-23 14:42:32 +02:00
|
|
|
});
|
|
|
|
}
|