66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
import goStore, { oauth2_register_discord, setup_websocket, heron_cookie_login, join_game } from "@/netcode";
|
|
|
|
export default defineNuxtRouteMiddleware
|
|
(async (to, from) => {
|
|
await setup_websocket()
|
|
|
|
|
|
if (to.path == '/login') {
|
|
return
|
|
}
|
|
|
|
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("/games")) {
|
|
if (Array.isArray(to.params.id)) { return navigateTo('/lobby') }
|
|
try {
|
|
await join_game(to.params.id)
|
|
return
|
|
} catch {
|
|
return navigateTo("/lobby")
|
|
}
|
|
}
|
|
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('/')
|
|
}
|
|
}) |