Vue Router Authentication

December 15, 2019

Refer Setup Firebase Email Authentication (Web) with Whitelisted emails using Vue.js and Vue Router.

Edit main.js.

const app = {
  name: 'My App',
  version: '1.0.1',
  isAuthenticated: false
}

Vue.prototype.$app = app

// TODO: check is authenticated
// app.isAuthenticated = true

Edit router/index.js.

router.beforeEach((to, from, next) => {
  if (to.fullPath === '/about') {
    next()
    return
  }

  const isAuthenticated = Vue.prototype.$app.isAuthenticated
  if (!isAuthenticated && to.fullPath !== '/signin') next('/signin')
  else next()
})
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.