Skip to content

Instantly share code, notes, and snippets.

@romines
Last active May 24, 2018 04:23
Show Gist options
  • Save romines/61b26a4a177281ee1b61964604dc6dfe to your computer and use it in GitHub Desktop.
Save romines/61b26a4a177281ee1b61964604dc6dfe to your computer and use it in GitHub Desktop.
import store from '../store'
import VueRouter from 'vue-router'
import Firebase from '../firebaseInit.js'
import Home from '../components/Home'
import Login from '../components/Login'
import SignUp from '../components/SignUp'
import Resorts from '../components/Resorts'
import ExportJson from '../components/ExportJson'
const routes = [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/resorts',
name: 'resorts',
component: Resorts
},
{
path: '/login',
name: 'login',
component: Login
},
{
path: '/sign-up/:encodedResortId',
name: 'sign-up',
component: SignUp,
props: true
},
{
path: '/json',
component: ExportJson
},
]
const router = new VueRouter({ routes })
router.beforeEach((to, from, next) => {
store.commit('SET_FB_REFS', Firebase)
if (to.name === 'login' || to.name === 'sign-up' || to.params.adminRedirect) {
store.commit('SET_LOADING_STATE', false)
return next()
}
if (!Firebase.auth().currentUser) {
return next('/login')
} else {
store.dispatch('getUserData', Firebase.auth().currentUser).then(() => {
if (store.state.user.superAdmin) {
store.dispatch('getResorts').then(() => next({ name: 'resorts', params: { adminRedirect: true }}))
} else {
store.dispatch('listenToContacts').then(() => next())
}
})
}
})
// or . . .
router.beforeEach((to, from, next) => {
const nextRouteNotIdentical = (to != from)
if (nextRouteNotIdentical) {
const currentUser = Firebase.auth().currentUser
if(currentUser) {
const { superAdmin } = await store.dispatch('getUserData', currentUser)
if(superAdmin) {
store.dispatch('getResorts').then(() => next({ name: 'resorts', params: { adminRedirect: true }}))
} else {
store.dispatch('listenToContacts').then(() => next())
}
} else {
next('/login')
}
}
})
export default router
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment