Skip to content

Commit

Permalink
chore: improve frontend route gating
Browse files Browse the repository at this point in the history
Improves the frontend route gating to not show the home page when the
auth hasn't been checked
  • Loading branch information
BobyMCbobs committed Oct 31, 2024
1 parent b63dfb8 commit 42ea691
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 @@ -1228,18 +1228,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 42ea691

Please sign in to comment.