Attempt to fix authenticate + local user data
[vchess.git] / client / src / router.js
1 import Vue from "vue";
2 import Router from "vue-router";
3 import Hall from "./views/Hall.vue";
4
5 Vue.use(Router);
6
7 function loadView(view) {
8 return () => import(/* webpackChunkName: "view-[request]" */ `@/views/${view}.vue`)
9 }
10
11 import { ajax } from "@/utils/ajax";
12 import { store } from "@/store";
13
14 export default new Router({
15 routes: [
16 {
17 path: "/",
18 name: "hall",
19 component: Hall,
20 },
21 {
22 path: "/variants",
23 name: "variants",
24 component: loadView("Variants"),
25 },
26 {
27 path: "/variants/:vname([a-zA-Z0-9]+)",
28 name: "rules",
29 component: loadView("Rules"),
30 },
31 {
32 path: "/authenticate/:token",
33 name: "authenticate",
34 beforeEnter: (to, from, next) => {
35 console.log("ajax call authenticate");
36 ajax(
37 "/authenticate",
38 "GET",
39 {token: to.params["token"]},
40 (res) => {
41 console.log(res);
42 store.state.user.id = res.id;
43 store.state.user.name = res.name;
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();
48 }
49 );
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;
62 store.state.user.name = "";
63 store.state.user.email = "";
64 store.state.user.notify = false;
65 next();
66 }
67 );
68 },
69 redirect: "/",
70 },
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 // },
82 // TODO: gameRef, problemId: https://router.vuejs.org/guide/essentials/dynamic-matching.html
83 ]
84 });