Attempt to fix authenticate + local user data
[vchess.git] / client / src / router.js
CommitLineData
625022fd
BA
1import Vue from "vue";
2import Router from "vue-router";
cf2343ce 3import Hall from "./views/Hall.vue";
625022fd
BA
4
5Vue.use(Router);
6
7function loadView(view) {
8 return () => import(/* webpackChunkName: "view-[request]" */ `@/views/${view}.vue`)
9}
10
1aeed627
BA
11import { ajax } from "@/utils/ajax";
12import { store } from "@/store";
13
625022fd
BA
14export default new Router({
15 routes: [
16 {
17 path: "/",
cf2343ce
BA
18 name: "hall",
19 component: Hall,
625022fd 20 },
5b020e73
BA
21 {
22 path: "/variants",
23 name: "variants",
24 component: loadView("Variants"),
25 },
cf2343ce
BA
26 {
27 path: "/variants/:vname([a-zA-Z0-9]+)",
28 name: "rules",
29 component: loadView("Rules"),
30 },
1aeed627
BA
31 {
32 path: "/authenticate/:token",
33 name: "authenticate",
34 beforeEnter: (to, from, next) => {
a7f9f050 35 console.log("ajax call authenticate");
1aeed627
BA
36 ajax(
37 "/authenticate",
38 "GET",
39 {token: to.params["token"]},
40 (res) => {
a7f9f050 41 console.log(res);
1aeed627
BA
42 store.state.user.id = res.id;
43 store.state.user.name = res.name;
a7f9f050
BA
44 store.state.user.email = res.email;
45 store.state.user.notify = res.notify;
46 // NOTE: mysid isn't cleared (required for potential game continuation)
47 next();
1aeed627
BA
48 }
49 );
1aeed627
BA
50 },
51 redirect: "/",
52 },
53 {
54 path: "/logout",
55 name: "logout",
56 beforeEnter: (to, from, next) => {
57 ajax(
58 "/logout",
59 "GET",
60 () => {
61 store.state.user.id = 0;
a7f9f050
BA
62 store.state.user.name = "";
63 store.state.user.email = "";
64 store.state.user.notify = false;
65 next();
1aeed627
BA
66 }
67 );
1aeed627
BA
68 },
69 redirect: "/",
70 },
ccd4a2b7
BA
71// {
72// path: "/about",
73// name: "about",
74// // route level code-splitting
75// // this generates a separate chunk (about.[hash].js) for this route
76// // which is lazy-loaded when the route is visited.
77// component: loadView('About'),
78// //function() {
79// // return import(/* webpackChunkName: "about" */ "./views/About.vue");
80// //}
81// },
8d61fc4a 82 // TODO: gameRef, problemId: https://router.vuejs.org/guide/essentials/dynamic-matching.html
625022fd
BA
83 ]
84});