-
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathrouter.js
More file actions
77 lines (71 loc) · 1.83 KB
/
router.js
File metadata and controls
77 lines (71 loc) · 1.83 KB
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
import * as constants from "./constants.js";
import { createRouter, createWebHistory } from "vue-router";
import { authCheck } from "./api.js";
const router = createRouter({
history: createWebHistory(""),
routes: [
{
path: "/",
name: "home",
component: () => import("./views/Home.vue"),
},
{
path: "/login",
name: "login",
component: () => import("./views/LogIn.vue"),
props: (route) => ({ redirect: route.query[constants.params.redirect] }),
},
{
path: "/note/:title",
name: "note",
component: () => import("./views/Note.vue"),
props: true,
},
{
path: "/new",
name: "new",
component: () => import("./views/Note.vue"),
},
{
path: "/search",
name: "search",
component: () => import("./views/SearchResults.vue"),
props: (route) => ({
searchTerm: route.query[constants.params.searchTerm],
sortBy: Number(route.query[constants.params.sortBy]) || undefined,
}),
},
],
});
// Check the user is authenticated on first navigation (unless going to login)
let authChecked = false;
router.beforeEach(async (to) => {
if (authChecked || to.name === "login") {
return;
}
try {
await authCheck();
return;
} catch (error) {
if (error.response && error.response.status === 401) {
return {
name: "login",
query: { [constants.params.redirect]: to.fullPath },
};
}
} finally {
authChecked = true;
}
});
router.afterEach((to) => {
let title = "flatnotes";
if (to.name === "note") {
if (to.params.title) {
title = `${to.params.title} - ${title}`;
} else {
title = "New Note - " + title;
}
}
document.title = title;
});
export default router;