Skip to content

Instantly share code, notes, and snippets.

@Timtech4u
Created September 24, 2018 17:19
Show Gist options
  • Select an option

  • Save Timtech4u/f6dbd2228deca8ba69503e65569d97ec to your computer and use it in GitHub Desktop.

Select an option

Save Timtech4u/f6dbd2228deca8ba69503e65569d97ec to your computer and use it in GitHub Desktop.
Vuejs ACL Using Firebase Auth + Vue Router
//1. Can be used in Navbar/base filr
import firebase from 'firebase';
export default {
name: 'navbar',
data() {
return {
isLoggedIn: false,
currentUser: false
};
},
created() {
if (firebase.auth().currentUser) {
this.isLoggedIn = true;
this.currentUser = firebase.auth().currentUser.email;
}
},
methods: {
logout: function() {
firebase
.auth()
.signOut()
.then(() => {
this.$router.go({ path: this.$router.path });
});
}
}
};
//2. Array of routes contains:
{
path: '/',
name: 'Home',
component: Home,
meta: {
requiresAuth: true
}
},
{
path: '/login',
name: 'login',
component: Login,
meta: {
requiresGuest: true
}
},
//3. Nav Guard
router.beforeEach((to, from, next) => {
// Check for requiresAuth guard
if (to.matched.some(record => record.meta.requiresAuth)) {
// Check if NO logged user
if (!firebase.auth().currentUser) {
// Go to login
next({
path: '/login',
query: {
redirect: to.fullPath
}
});
} else {
// Proceed to route
next();
}
} else if (to.matched.some(record => record.meta.requiresGuest)) {
// Check if NO logged user
if (firebase.auth().currentUser) {
// Go to login
next({
path: '/',
query: {
redirect: to.fullPath
}
});
} else {
// Proceed to route
next();
}
} else {
// Proceed to route
next();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment