2022-05-30 20:19:41 +02:00
|
|
|
import { logout } from "@/services/authService";
|
|
|
|
import { getMutualGuilds } from "@/services/guildService";
|
|
|
|
import { useMutualGuildsStore } from "@/stores/mutualGuilds";
|
2022-05-21 01:07:31 +02:00
|
|
|
import { useUserStore } from "@/stores/user";
|
2022-05-30 20:19:41 +02:00
|
|
|
import GuildHomeViewVue from "@/views/GuildHomeView.vue";
|
2022-05-21 01:07:31 +02:00
|
|
|
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;
|
2022-06-12 12:51:05 +02:00
|
|
|
title: string;
|
2022-05-21 01:07:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
|
|
routes: [
|
|
|
|
{
|
|
|
|
path: "/",
|
|
|
|
name: "home",
|
|
|
|
component: HomeView,
|
|
|
|
meta: {
|
2022-05-24 23:04:35 +02:00
|
|
|
requiresAuth: false,
|
2022-06-12 12:51:05 +02:00
|
|
|
title: "Home",
|
2022-05-21 01:07:31 +02:00
|
|
|
},
|
|
|
|
},
|
2022-05-30 20:19:41 +02:00
|
|
|
{
|
|
|
|
path: "/guild/:guildId",
|
|
|
|
name: "guildHome",
|
|
|
|
component: GuildHomeViewVue,
|
|
|
|
meta: {
|
|
|
|
requiresAuth: true,
|
2022-06-12 12:51:05 +02:00
|
|
|
title: "Home",
|
2022-05-30 20:19:41 +02:00
|
|
|
},
|
|
|
|
},
|
2022-05-21 01:07:31 +02:00
|
|
|
{
|
|
|
|
path: "/oauth2/callback",
|
|
|
|
name: "oauth-callback",
|
|
|
|
component: OauthCallbackViewVue,
|
|
|
|
meta: {
|
|
|
|
requiresAuth: false,
|
2022-06-12 12:51:05 +02:00
|
|
|
title: "Login",
|
2022-05-21 01:07:31 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/oauth2/redirect",
|
|
|
|
name: "oauth-redirect",
|
|
|
|
component: OauthRedirectViewVue,
|
|
|
|
meta: {
|
|
|
|
requiresAuth: false,
|
2022-06-12 12:51:05 +02:00
|
|
|
title: "Login",
|
2022-05-21 01:07:31 +02:00
|
|
|
},
|
2022-05-22 16:25:13 +02:00
|
|
|
},
|
2022-05-21 01:07:31 +02:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
router.beforeEach((to, from) => {
|
|
|
|
const store = useUserStore();
|
2022-06-12 12:51:05 +02:00
|
|
|
document.title = `${import.meta.env.VITE_APP_TITLE} - ${to.meta.title}`;
|
2022-05-21 01:07:31 +02:00
|
|
|
if (to.meta.requiresAuth && !store.isLoggedIn) {
|
2022-05-30 20:19:41 +02:00
|
|
|
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" });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-05-21 01:07:31 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default router;
|