Merge branch 'feature/route-authentication'
This commit is contained in:
commit
52bcd1704f
@ -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
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user