Cleanup global state logic a bit, for current user and permissions

this still isn't ideal perhaps, but leaves some clues for improvement
This commit is contained in:
Lance Edgar 2019-11-12 11:49:07 -06:00
parent eab0ebcc00
commit 84b2a2bf70
3 changed files with 33 additions and 26 deletions

View file

@ -20,7 +20,11 @@ export default {
props: { props: {
appsettings: Object, appsettings: Object,
}, },
created: function() { beforeCreate: function() {
// TODO: even with beforeCreate() this logic is still happening "too
// slowly" for us to reliably check perms on first page load,
// i.e. after browser refresh. maybe can avoid async for the GET?
// when main app is first created, fetch user session data from // when main app is first created, fetch user session data from
// backend; this will tell us who the user is (or if they're not yet // backend; this will tell us who the user is (or if they're not yet
@ -30,10 +34,10 @@ export default {
this.$http.get('/api/session').then(response => { this.$http.get('/api/session').then(response => {
// let all of app know who the user is(n't) // let all of app know who the user is(n't)
this.$store.commit('setUser', response.data.user) this.$store.commit('SET_USER', response.data.user)
// also keep track of user's permissions // also keep track of user's permissions
this.$store.commit('setPermissions', response.data.permissions) this.$store.commit('SET_PERMISSIONS', response.data.permissions)
// if user is anonymous, and requested logout page, send to login instead // if user is anonymous, and requested logout page, send to login instead
if (!response.data.user && this.$route.name == 'logout') { if (!response.data.user && this.$route.name == 'logout') {
@ -47,10 +51,6 @@ export default {
}, },
mounted: function () { mounted: function () {
// // cache the app version
// // this.$store.commit('setAppVersion', appsettings.version);
// this.$store.commit('setAppVersion', this.appsettings.version);
// add "testing" watermark unless running in production mode // add "testing" watermark unless running in production mode
// if (!appsettings.production) { // if (!appsettings.production) {
if (!this.appsettings.production) { if (!this.appsettings.production) {

View file

@ -174,16 +174,24 @@ export default {
mounted() { mounted() {
// redirect if user doesn't have permission to be here // TODO: this seems like a "good" idea, but in practice, when reloading
if ((this.mode == 'creating' && !this.hasModelPerm('create')) // the page/app via browser (Ctrl+Shift+R), the app must re-fetch the
|| (this.mode == 'editing' && !this.hasModelPerm('edit'))) { // session details before it knows which user/permissions are in
this.$buefy.toast.open({ // effect, and that takes "too long" which means these checks fail!
message: "You do not have permission to access that page.",
type: 'is-danger', // // redirect if user doesn't have permission to be here
position: 'is-bottom', // if ((this.mode == 'viewing' && !this.hasModelPerm('view'))
}) // || (this.mode == 'creating' && !this.hasModelPerm('create'))
this.$router.push(this.getModelPathPrefix() + '/') // || (this.mode == 'editing' && !this.hasModelPerm('edit'))
} // || (this.mode == 'deleting' && !this.hasModelPerm('delete'))) {
// this.$buefy.toast.open({
// message: "You do not have permission to access that page.",
// type: 'is-danger',
// position: 'is-bottom',
// })
// this.$router.push(this.getModelPathPrefix() + '/')
// return
// }
// fetch initial page data unless 'creating' // fetch initial page data unless 'creating'
if (this.mode != 'creating') { if (this.mode != 'creating') {

View file

@ -1,20 +1,19 @@
export let ByjoveStoreConfig = { export let ByjoveStoreConfig = {
state: { state: {
appVersion: null,
user: null, user: null,
permissions: [], permissions: [],
}, },
mutations: { mutations: {
setAppVersion(state, payload) { SET_USER(state, user) {
state.appVersion = payload state.user = user
}, },
setUser(state, payload) { SET_PERMISSIONS(state, permissions) {
state.user = payload state.permissions = permissions
},
setPermissions(state, payload) {
state.permissions = payload
}, },
}, },
// actions: {}, actions: {
// TODO: should we define the logic here, for fetching current session
// from backend API? (thus far that happens in App.mount() instead)
},
} }