Demonstration on how to guard paths in a Vue project with Vue Router and Firebase Authentication
I have written a blog with explenation
Hosted at vue-routes-authentication.web.app
export function getCurrentUser() {
return new Promise((resolve, reject) => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
unsubscribe()
resolve(user)
}, reject)
})
}
const routes = [
{
path: '/',
redirect: '/signin'
},
{
path: '/signin',
component: SignIn
},
{
path: '/profile',
component: Profile,
meta: {
requiresAuth: true
}
}
]
router.beforeEach(async (to) => {
const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
if (requiresAuth && !await getCurrentUser()) {
return '/signin';
}
})
npm run dev
npm run deploy