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
|
import { createRouter, createWebHistory } from 'vue-router'
import { store } from '@/store.js'
import {
LoginView,
SubscriptionsView,
EpisodesView,
ForgotPasswordView,
SettingsView,
RegistrationView,
ResetPasswordView
} from '@/views'
import { useLogger } from '@/logger.js'
const logger = useLogger();
const routes = [
{
path: '/',
redirect: to => {
return store.isLoggedIn ? '/subscriptions' : '/login';
},
meta: { requiresAuth: false },
},
{
path: '/login',
name: 'Login',
component: LoginView,
meta: { requiresAuth: false },
},
{
path: '/forgotPassword',
name: 'ForgotPassword',
component: ForgotPasswordView,
meta: { requiresAuth: false },
},
{
path: '/registration',
name: 'Registration',
component: RegistrationView,
meta: { requiresAuth: false },
},
{
path: '/resetPassword',
name: 'ResetPassword',
component: ResetPasswordView,
props: router => ({
token: router.query.token,
username: router.query.username
}),
meta: { requiresAuth: false },
},
{
path: '/subscriptions',
name: 'Subscriptions',
component: SubscriptionsView,
meta: { requiresAuth: true }
},
{
path: '/episodes',
name: 'Episodes',
component: EpisodesView,
meta: { requiresAuth: true }
},
{
path: '/settings',
name: 'Settings',
component: SettingsView,
meta: { requiresAuth: true }
},
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
redirect: to => {
logger.pageNotFound();
return "/";
},
meta: { requiresAuth: false },
}
]
const baseURL = import.meta.env.BASE_URL || "/";
console.log("Base-URL", baseURL);
const router = createRouter({
history: createWebHistory(baseURL),
routes
})
router.beforeEach((to, from, next) => {
// instead of having to check every route record with
// to.matched.some(record => record.meta.requiresAuth)
if (to.meta.requiresAuth && !store.isLoggedIn) {
// this route requires auth, check if logged in
// if not, redirect to login page.
next({
path: '/',
// save the location we were at to come back later
query: { redirect: to.fullPath },
});
} else if (!to.meta.requiresAuth && store.isLoggedIn) {
next({
path: '/'
});
} else if (store.isLoggedIn && from.query.redirect) {
// user is logged in and there's a saved location in the query
// redirect them to that location
const redirect = from.query.redirect;
delete from.query.redirect;
next(redirect);
} else {
next();
}
});
export default router
|