Skip to content

Instantly share code, notes, and snippets.

@garbinmarcelo
Created July 29, 2020 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save garbinmarcelo/e6bb671ba07c02bc172f4feb4a2106c5 to your computer and use it in GitHub Desktop.
Save garbinmarcelo/e6bb671ba07c02bc172f4feb4a2106c5 to your computer and use it in GitHub Desktop.
Vue Router beforeEach.js
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