Claptrap-ui/src/router/index.ts
SebClem 82b2148d86
🔨 Add server list
🔨 Add server list
2022-06-11 19:53:07 +02:00

79 lines
2.0 KiB
TypeScript

import { logout } from "@/services/authService";
import { getMutualGuilds } from "@/services/guildService";
import { useMutualGuildsStore } from "@/stores/mutualGuilds";
import { useUserStore } from "@/stores/user";
import GuildHomeViewVue from "@/views/GuildHomeView.vue";
import OauthCallbackViewVue from "@/views/oauth/OauthCallbackView.vue";
import OauthRedirectViewVue from "@/views/oauth/OauthRedirectView.vue";
import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
declare module "vue-router" {
interface RouteMeta {
requiresAuth: boolean;
}
}
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: "/",
name: "home",
component: HomeView,
meta: {
requiresAuth: false,
},
},
{
path: "/guild/:guildId",
name: "guildHome",
component: GuildHomeViewVue,
meta: {
requiresAuth: true,
},
},
{
path: "/oauth2/callback",
name: "oauth-callback",
component: OauthCallbackViewVue,
meta: {
requiresAuth: false,
},
},
{
path: "/oauth2/redirect",
name: "oauth-redirect",
component: OauthRedirectViewVue,
meta: {
requiresAuth: false,
},
},
],
});
router.beforeEach((to, from) => {
const store = useUserStore();
if (to.meta.requiresAuth && !store.isLoggedIn) {
return { name: "oauth-redirect" };
} else {
const mutualGuildsStore = useMutualGuildsStore();
if (store.isLoggedIn) {
getMutualGuilds()
.then((value) => {
mutualGuildsStore.guilds = value;
mutualGuildsStore.loaded = true;
})
.catch((reason) => {
if (reason?.response.status == 401) {
console.log("401, Login expired, logout...");
logout(true, false);
router.push({ name: "home" });
}
});
}
}
});
export default router;