Skip to content

Commit

Permalink
Merge branch 'improve-route-gating' into 'main'
Browse files Browse the repository at this point in the history
chore: improve frontend route gating

See merge request flattrack/flattrack!403
  • Loading branch information
BobyMCbobs committed Oct 31, 2024
2 parents 22e5315 + 42ea691 commit be9dd26
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
12 changes: 12 additions & 0 deletions web/src/common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ function SetAuthToken (token) {
return localStorage.setItem('authToken', token)
}

// HasAuthToken
// returns whether an auth token is found
function HasAuthToken () {
const authToken = GetAuthToken()
return !(
typeof authToken === 'undefined' ||
authToken === null ||
authToken === ''
)
}

// DeleteAuthToken
// removes the JWT from localStorage
function DeleteAuthToken () {
Expand Down Expand Up @@ -203,6 +214,7 @@ function CopyHrefToClipboard () {
export default {
GetAuthToken,
SetAuthToken,
HasAuthToken,
DeleteAuthToken,
DisplaySuccessToast,
DisplayFailureToast,
Expand Down
14 changes: 7 additions & 7 deletions web/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import routerCommon from './common'
import common from '../common/common'

Vue.use(VueRouter)

Expand All @@ -18,20 +19,19 @@ const router = new VueRouter({
}
})

router.beforeEach((to, from, next) => {
router.beforeEach(async (to, from, next) => {
if (typeof to.name !== 'undefined') {
document.title = `${to.name} | FlatTrack`
}
if (to.matched.some(route => route.meta.requiresAuth === true)) {
if (to.meta.requiresAuth === true && common.HasAuthToken() === false) {
routerCommon.requireAuthToken(to, from, next)
}
if (to.matched.some(route => route.meta.requiresNoAuth === true)) {
} else if (to.meta.requiresNoAuth === true) {
routerCommon.requireNoAuthToken(to, from, next)
}
if (to.matched.some(route => route.meta.requiresGroup)) {
} else if (typeof to.requiresGroup !== 'undefined') {
routerCommon.requireGroup(to, from, next)
} else {
next()
}
next()
})

export default router
6 changes: 3 additions & 3 deletions web/src/views/authenticated/shopping-list-view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1229,18 +1229,18 @@ export default {
this.templateListName = resp.data.spec.name
})
},
isNewItemModalActive() {
isNewItemModalActive () {
if (this.isNewItemModalActive !== false) {
return
}
this.GetShoppingListItems()
},
isEditItemModalActive() {
isEditItemModalActive () {
if (this.isEditItemModalActive !== false) {
return
}
this.GetShoppingListItems()
},
}
},
async beforeMount () {
this.GetShoppingList()
Expand Down

0 comments on commit be9dd26

Please sign in to comment.