forked from DeepNotesApp/DeepNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouting.ts
More file actions
89 lines (72 loc) · 2.2 KB
/
Copy pathrouting.ts
File metadata and controls
89 lines (72 loc) · 2.2 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
78
79
80
81
82
83
84
85
86
87
88
89
import { isIncluded } from '@stdlib/misc';
import type { AuthStore } from 'src/stores/auth';
import type { RouteLocationNormalized, Router } from 'vue-router';
import { trpcClient } from './trpc';
import { getRequestConfig } from './utils/misc';
const moduleLogger = mainLogger.sub('routing.universal.ts');
export async function redirectIfNecessary(input: {
router: Router;
route: RouteLocationNormalized;
auth: AuthStore;
cookies?: typeof Cookies;
}) {
const redirectDest = await getRedirectDest({
route: input.route,
auth: input.auth,
cookies: input.cookies,
});
if (redirectDest != null) {
moduleLogger.info(
'redirectIfNecessary redirect: %s',
JSON.stringify(redirectDest),
);
await input.router.replace(redirectDest);
}
}
export async function getRedirectDest(input: {
route: RouteLocationNormalized;
auth: AuthStore;
cookies?: typeof Cookies;
}) {
// Page requires auth
if (
!input.auth.loggedIn &&
input.route.matched.some((record) => record.meta.requiresAuth)
) {
return { name: 'login', query: { redirect: input.route.fullPath } };
}
// Page requires guest
if (
input.auth.loggedIn &&
input.route.matched.some((record) => record.meta.requiresGuest)
) {
return {
name: isIncluded(process.env.MODE, ['ssr', 'spa']) ? 'home' : 'pages',
};
}
// Starting page redirection
if (input.auth.loggedIn && input.route.name === 'pages') {
try {
const startingPageId =
await trpcClient.users.pages.getStartingPageId.query(undefined, {
context: getRequestConfig(input.cookies),
});
return { name: 'page', params: { pageId: startingPageId } };
} catch (error) {
moduleLogger.error('getRedirectDest error: %o', error);
return { name: 'home' };
}
}
// Group main page redirection
if (input.route.name === 'group') {
await trpcClient.groups.getMainPageId.query({
groupId: input.route.params.groupId as string,
});
const mainPageId = await trpcClient.groups.getMainPageId.query({
groupId: input.route.params.groupId as string,
});
if (mainPageId != null) {
return { name: 'page', params: { pageId: mainPageId } };
}
}
}