setup route for first user
This commit is contained in:
parent
91ff0835fb
commit
6d9e75bb0c
20 changed files with 455 additions and 30 deletions
|
@ -29,29 +29,34 @@ export async function isAuthenticatedPromise(): Promise<Payload> {
|
|||
return new Promise<Payload>(async (resolve, reject) => {
|
||||
const auth = useAuthStore();
|
||||
const account = useAccountStore();
|
||||
let decoded = jwtDecode<Payload>(localStorage.getItem("accessToken") ?? "");
|
||||
let decoded: Payload | string = "";
|
||||
try {
|
||||
decoded = jwtDecode<Payload>(localStorage.getItem("accessToken") ?? "");
|
||||
} catch (error) {
|
||||
reject("failed");
|
||||
}
|
||||
|
||||
auth.setSuccess();
|
||||
if (typeof decoded == "string" || !decoded) {
|
||||
reject("failed");
|
||||
}
|
||||
} else {
|
||||
// check jwt expiry
|
||||
const exp = decoded.exp ?? 0;
|
||||
const localTimezoneOffset = new Date().getTimezoneOffset();
|
||||
const correctedLocalTime = new Date().getTime() + localTimezoneOffset * 60000;
|
||||
if (exp < Math.floor(correctedLocalTime / 1000)) {
|
||||
await refreshToken()
|
||||
.then(() => {
|
||||
console.log("fetched new token");
|
||||
})
|
||||
.catch(() => {
|
||||
reject("expired");
|
||||
});
|
||||
}
|
||||
|
||||
// check jwt expiry
|
||||
const exp = decoded.exp ?? 0;
|
||||
const localTimezoneOffset = new Date().getTimezoneOffset();
|
||||
const correctedLocalTime = new Date().getTime() + localTimezoneOffset * 60000;
|
||||
if (exp < Math.floor(correctedLocalTime / 1000)) {
|
||||
await refreshToken()
|
||||
.then(() => {
|
||||
console.log("fetched new token");
|
||||
})
|
||||
.catch(() => {
|
||||
reject("expired");
|
||||
});
|
||||
var { firstname, lastname, mail, username } = decoded;
|
||||
account.setAccountData(firstname, lastname, mail, username);
|
||||
resolve(decoded);
|
||||
}
|
||||
|
||||
var { firstname, lastname, mail, username } = decoded;
|
||||
account.setAccountData(firstname, lastname, mail, username);
|
||||
resolve(decoded);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import Login from "../views/Login.vue";
|
|||
|
||||
import { isAuthenticated } from "./authGuards";
|
||||
import { loadAccountData } from "./accountGuard";
|
||||
import { isSetup } from "./setupGuard";
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
|
@ -16,6 +17,25 @@ const router = createRouter({
|
|||
name: "login",
|
||||
component: Login,
|
||||
},
|
||||
{
|
||||
path: "/setup",
|
||||
name: "setup",
|
||||
component: () => import("../views/RouterView.vue"),
|
||||
beforeEnter: [isSetup],
|
||||
children: [
|
||||
{
|
||||
path: "",
|
||||
name: "setup-create",
|
||||
component: () => import("../views/setup/Setup.vue"),
|
||||
},
|
||||
{
|
||||
path: "verify",
|
||||
name: "setup-verify",
|
||||
component: () => import("../views/setup/Verify.vue"),
|
||||
props: (route) => ({ mail: route.query.mail, token: route.query.token }),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: "/admin",
|
||||
name: "admin",
|
||||
|
|
16
src/router/setupGuard.ts
Normal file
16
src/router/setupGuard.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
import NProgress from "nprogress";
|
||||
import { http } from "../serverCom";
|
||||
|
||||
export async function isSetup(to: any, from: any, next: any) {
|
||||
NProgress.start();
|
||||
await http
|
||||
.get("/setup")
|
||||
.then(() => {
|
||||
NProgress.done();
|
||||
next();
|
||||
})
|
||||
.catch(() => {
|
||||
NProgress.done();
|
||||
next({ name: "login" });
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue