Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
router.beforeEach((routeTo, routeFrom, next) => {
// If this isn't an initial page load...
if (routeFrom.name !== null) {
// Start the route progress bar.
NProgress.start()
}
// Check if auth is required on this route
// (including nested routes).
const authRequired = routeTo.matched.some((route) => route.meta.authRequired)
// If auth isn't required for the route, just continue.
if (!authRequired) return next()
// If auth is required and the user is logged in...
if (store.getters['auth/loggedIn']) {
// Validate the local user token...
return store.dispatch('auth/validate').then((validUser) => {
// Then continue if the token still represents a valid user,
// otherwise redirect to login.
validUser ? next() : redirectToLogin()
router.beforeResolve((routeTo, routeFrom, next) => {
// If this isn't an initial page load...
if (routeFrom.name) {
// Start the route progress bar.
NProgress.start()
}
next()
})
router.beforeEach(async (routeTo, routeFrom, next) => {
// If this isn't an initial page load...
if (routeFrom.name !== null) {
// Start the route progress bar.
NProgress.start()
}
if (['login', 'logout', '404'].includes(routeTo.name)) return next()
// Check if session is valid and renew if necessary.
// Fetch user asynchronously after checking session.
const userPromise = checkSession().then(getCurrentUser)
// If auth isn't required for the route (or nested routes),
// just continue and don't wait for user object.
if (!routeTo.matched.some(route => route.meta.authRequired)) return next()
// Wait for user object to check permissions
const user = await userPromise
if (!user || user.role === 'TODO') {
writeValue(endpoint, key, value) {
NProgress.start();
const beforeTime = new Date();
let afterTime = null;
return fetch(`${endpoint}/${key}`, {
method: 'POST',
body: JSON.stringify({value: value}),
})
.then((response) => {
NProgress.done();
afterTime = new Date();
return response.json()
}).then((data) => {
data.elapsedTime = afterTime.getTime() - beforeTime.getTime();
return data;
});
}
readKey(endpoint, key) {
NProgress.start();
const beforeTime = new Date();
let afterTime = null;
return fetch(`${endpoint}/${key}`)
.then((response) => {
NProgress.done();
afterTime = new Date();
return response.json()
}).then((data) => {
data.elapsedTime = afterTime.getTime() - beforeTime.getTime();
return data;
});
}