Files
front/middleware/auth.global.ts
T

76 lines
1.9 KiB
TypeScript

import { useAuthStore } from "#imports";
export default defineNuxtRouteMiddleware(async (to, from) => {
const authStore = useAuthStore();
const config = useRuntimeConfig();
if (to.path == "/login") {
const code = to.query.code;
if (!code) {
return navigateTo(config.public.discordLoginEndpoint, { external: true });
}
if (!Array.isArray(code)) {
try {
await authStore.whoami();
return navigateTo("/lobby");
} catch {
return navigateTo("/login");
}
} else {
return navigateTo("/login");
}
}
// if (to.path == '/') {
// const code = to.query.code
// if (code) {
// if (!Array.isArray(code)) {
// try {
// await oauth2_register_discord(code)
// return navigateTo('/lobby')
// } catch {
// return navigateTo('/login')
// }
// } else {
// return navigateTo('/login')
// }
// }
// return navigateTo("/lobby")
// }
// if (!goStore.discordId) {
// const heron_session = useCookie('heron_session')
// if (!heron_session.value) { return navigateTo('/login') }
// try {
// await heron_cookie_login(heron_session.value)
// if (to.path == '/') {
// return navigateTo("/lobby")
// }
// } catch {
// return navigateTo('/login')
// }
// }
if (to.path.startsWith("/game")) {
if (Array.isArray(to.params.id)) {
return navigateTo("/lobby");
}
return;
}
if (to.path == "/lobby") {
return;
}
if (to.path == "/rules") {
return;
}
// In a real app you would probably not redirect every route to `/`
// however it is important to check `to.path` before redirecting or you
// might get an infinite redirect loop
if (to.path !== "/") {
return navigateTo("/");
}
return navigateTo("/lobby");
});