Created
July 29, 2020 12:19
-
-
Save garbinmarcelo/e6bb671ba07c02bc172f4feb4a2106c5 to your computer and use it in GitHub Desktop.
Vue Router beforeEach.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import store from '@/store' | |
const route = { | |
login: 'login', | |
dashboard: 'dashboard' | |
} | |
export default async (to, from, next) => { | |
// Set head title | |
document.title = (to.meta.title) ? `${to.meta.title} / ${process.env.VUE_APP_FULL_NAME}` : process.env.VUE_APP_FULL_NAME | |
if(to.meta.auth || (to.name !== 'login' && !store.getters['authentication/hasToken'])){ | |
try{ | |
await store.dispatch('authentication/checkToken') | |
next() | |
}catch(error){ | |
next({name: route.login}) | |
} | |
}else if(to.name === 'login' && store.getters['auth/hasToken']){ | |
next({name: route.dashboard}) | |
}else{ | |
next() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment