Skip to content

Commit

Permalink
feat(auth): Add navigation guards
Browse files Browse the repository at this point in the history
  • Loading branch information
bikingbadger committed Mar 3, 2021
1 parent af40109 commit c1d2194
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
23 changes: 23 additions & 0 deletions src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createRouter, createWebHistory } from 'vue-router';
import firebase from '@/utilities/firebase';
import Home from '../views/Home.vue';

const routes = [
Expand All @@ -10,6 +11,9 @@ const routes = [
path: '/home',
name: 'home',
component: Home,
meta: {
requiresAuth: true,
},
},
{
path: '/about',
Expand All @@ -18,6 +22,9 @@ const routes = [
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
meta: {
requiresAuth: true,
},
},
{
path: '/login',
Expand All @@ -26,6 +33,9 @@ const routes = [
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue'),
meta: {
requiresNoAuth: true,
},
},
{
path: '/register',
Expand All @@ -34,6 +44,9 @@ const routes = [
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/Register.vue'),
meta: {
requiresNoAuth: true,
},
},
];

Expand All @@ -42,4 +55,14 @@ const router = createRouter({
routes,
});

router.beforeEach(async (to, from, next) => {
if (to.meta.requiresAuth && !firebase.auth().currentUser) {
next('login');
} else if (to.meta.requiresNoAuth && firebase.auth().currentUser) {
next('home');
} else {
next();
}
});

export default router;
4 changes: 2 additions & 2 deletions src/store/auth/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
state.email = userData.email;
state.registrationError = false;
state.errorMsg = '';
router.push({ path: 'home' });
router.replace({ path: 'home' });
},

authFail(state, payload) {
Expand All @@ -19,7 +19,7 @@ export default {
logout(state) {
state.isLoggedIn = false;
state.currentUser = '';
router.push({ path: 'login' });
router.replace({ path: 'login' });
},

getUser(state, userData) {
Expand Down
14 changes: 7 additions & 7 deletions src/views/Register.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<Panel header="Register">
<label for="username">Username</label>
<InputText id="username" type="text" v-model="username" class="p-mb-2 p-d-block" />
<label for="email">Email</label>
<InputText id="email" type="email" v-model="email" class="p-mb-2 p-d-block" />
<label for="password">Password</label>
<Password id="password" v-model="password" :feedback="false" class="p-d-block" />
<Button label="Register" @click="registerUser" />
<label for="username">Username</label>
<InputText id="username" type="text" v-model="username" class="p-mb-2 p-d-block" />
<label for="email">Email</label>
<InputText id="email" type="email" v-model="email" class="p-mb-2 p-d-block" />
<label for="password">Password</label>
<Password id="password" v-model="password" :feedback="false" class="p-d-block" />
<Button label="Register" @click="registerUser" />
</Panel>
</template>

Expand Down

0 comments on commit c1d2194

Please sign in to comment.