-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathrouter.js
180 lines (166 loc) · 4.57 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '@/routes/home/HomeView.vue';
import LoginView from '@/routes/login/LoginView.vue';
import LobbyView from '@/routes/lobby/LobbyView.vue';
import GameView from '@/routes/game/GameView.vue';
import RulesView from '@/routes/rules/RulesView.vue';
import StatsView from '@/routes/stats/StatsView.vue';
import { useGameStore } from '@/stores/game';
import { useAuthStore } from '@/stores/auth';
export const ROUTE_NAME_GAME = 'Game';
export const ROUTE_NAME_SPECTATE = 'Spectate';
export const ROUTE_NAME_HOME = 'Home';
export const ROUTE_NAME_LOBBY = 'Lobby';
export const ROUTE_NAME_LOGIN = 'Login';
export const ROUTE_NAME_LOGOUT = 'Logout';
export const ROUTE_NAME_RULES = 'Rules';
export const ROUTE_NAME_SIGNUP = 'Signup';
export const ROUTE_NAME_STATS = 'Stats';
const mustBeAuthenticated = async (to, from, next) => {
const authStore = useAuthStore();
if (authStore.authenticated) {
return next();
}
const isReturningUser = await authStore.getIsReturningUser();
if (isReturningUser === 'true') {
return next('/login');
}
return next('/signup');
};
const logoutAndRedirect = async (to, from, next) => {
const authStore = useAuthStore();
await authStore.requestLogout();
return next('/login');
};
const checkAndSubscribeToLobby = async (to) => {
const gameStore = useGameStore();
const authStore = useAuthStore();
const gameId = parseInt(to.params.gameId);
try {
if (Number.isNaN(gameId) || !Number.isFinite(gameId)) {
throw new Error('home.snackbar.invalidLobbyNumber');
}
if (!authStore.authenticated) {
return { path: `/login/${gameId}` };
}
if (gameStore.players.some(({ username }) => username === authStore.username)) {
return true;
}
await gameStore.requestSubscribe(gameId);
return true;
} catch (err) {
return { name: 'Home', query: { gameId: gameId, error: err.message } };
}
};
const routes = [
{
path: '/',
name: 'Home',
component: HomeView,
beforeEnter: mustBeAuthenticated,
},
{
path: '/login/:lobbyRedirectId?',
name: ROUTE_NAME_LOGIN,
component: LoginView,
meta: {
hideNavigation: true,
},
},
{
path: '/signup',
name: ROUTE_NAME_SIGNUP,
component: LoginView,
meta: {
hideNavigation: true,
},
},
// This route is just a passthrough to make sure the user is fully logged out before putting
// them on the login screen
{
path: '/logout',
name: ROUTE_NAME_LOGOUT,
beforeEnter: logoutAndRedirect,
meta: {
hideNavigation: true,
},
},
{
path: '/rules',
name: ROUTE_NAME_RULES,
component: RulesView,
},
{
name: ROUTE_NAME_LOBBY,
path: '/lobby/:gameId?',
component: LobbyView,
// TODO: Add logic to redirect if a given game does not exist
beforeEnter: checkAndSubscribeToLobby,
meta: {
hideNavigation: true,
},
},
{
name: ROUTE_NAME_GAME,
path: '/game/:gameId?',
component: GameView,
// TODO: Add logic to redirect if a given game does not exist
// mustBeAuthenticated intentionally left off here
// If a user refreshes the relogin modal will fire and allow them to continue playing
meta: {
hideNavigation: true,
},
},
{
name: ROUTE_NAME_SPECTATE,
path: '/spectate/:gameId?',
component: GameView,
meta: {
hideNavigation: true,
},
},
{
path: '/stats/:seasonId?',
name: ROUTE_NAME_STATS,
component: StatsView,
beforeEnter: mustBeAuthenticated,
},
// Catch every other unsupported route
{
path: '/:pathMatch(.*)*',
name: 'Not Found',
component: () => import('@/routes/error/NotFoundView.vue'),
},
];
const getInitialPath = () => {
if (window.location.hash.startsWith('#/')) {
const path = window.location.hash.replace('#/', '');
window.location.hash = '';
return path;
}
return null;
};
const initialPath = getInitialPath();
if (initialPath) {
window.history.replaceState({}, '', initialPath);
}
const router = createRouter({
history: createWebHistory(),
routes,
scrollBehavior(to, _from, savedPosition) {
if (to.hash) {
return { el: to.hash, behavior: 'smooth' };
} else if (savedPosition) {
return savedPosition;
}
return { top: 0 };
},
});
router.beforeEach(async (to, _from, next) => {
const authStore = useAuthStore();
// Make sure we try and reestablish a player's session if one exists
// We do this before the route resolves to preempt the reauth/logout logic
await authStore.requestStatus(to);
next();
});
export default router;