Merge branch 'feature/route-authentication'

This commit is contained in:
Hubert Van De Walle 2020-04-13 00:20:24 +02:00
commit 52bcd1704f
2 changed files with 23 additions and 3 deletions

View File

@ -8,6 +8,8 @@ import NoteCreation from '../views/NoteCreation.vue'
Vue.use(VueRouter)
import store from '../store';
const routes = [
{
path: '/',
@ -27,7 +29,10 @@ const routes = [
{
path: '/note/new',
name: 'NoteCreation',
component: NoteCreation
component: NoteCreation,
meta: {
requiresAuth: true,
}
}
]
@ -37,4 +42,14 @@ const router = new VueRouter({
routes
})
router.beforeEach((to, from, next) => {
// If user is not signed in -> redirect to /signin
if (to.matched.some(record => record.meta.requiresAuth) && !store.getters.isLoggedIn) {
next({
path: '/signin',
params: {nextUrl: to.fullPath}
})
} else
next()
})
export default router

View File

@ -14,9 +14,14 @@ export default new Vuex.Store({
setToken(token)
},
clearToken(state) {
state.token = ''
state.token = null
clearToken()
}
},
actions: {}
actions: {},
getters: {
isLoggedIn(state) {
return state.token !== null;
}
}
})