Skip to content

Commit

Permalink
Refactor config store.
Browse files Browse the repository at this point in the history
  • Loading branch information
fergmac committed Jul 12, 2024
1 parent a293d3a commit 047a34f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
17 changes: 11 additions & 6 deletions app/frontend/src/common/components/Auth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ export default {
}
},
keyCloakLogin () {
this.keycloak.init().success(() => {
this.keycloak.login({ idpHint: this.config.sso_idp_hint }).success((authenticated) => {
this.keycloak.init()
.then(() => {
console.log("Keycloak initialized successfully", this);
return this.keycloak.login({ idpHint: this.config.sso_idp_hint });
}).then((authenticated) => {
console.log("keycloack login authenticated")
if (authenticated) {
ApiService.authHeader('JWT', this.keycloak.token)
if (window.localStorage) {
Expand All @@ -48,10 +52,10 @@ export default {
localStorage.setItem('idToken', this.keycloak.idToken)
}
}
}).error((e) => {
this.$store.commit(SET_ERROR, { error: 'Cannot contact SSO provider' })
})
})
}).catch((error) => {
console.error("Keycloak login failed", error);
this.$store.commit(SET_ERROR, { error: 'Cannot contact SSO provider' });
});
},
keyCloakLogout () {
// This should log the user out, but unfortunately does not delete the cookie storing the user
Expand All @@ -68,6 +72,7 @@ export default {
},
watch: {
keycloak (kc) {
console.log("keycloack watch: ", kc)
if (kc) {
if (window._paq && this.authenticated) {
window._paq.push(["setCustomVariable", 1, "userType", this.keycloak.tokenParsed.identity_provider]);
Expand Down
1 change: 1 addition & 0 deletions app/frontend/src/common/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default {
computed: {
...mapGetters(['userRoles', 'config']),
hasConfig () {
console.log("has config: ", Boolean(this.config))
return Boolean(this.config)
},
getEnvironmentMessage () {
Expand Down
21 changes: 10 additions & 11 deletions app/frontend/src/common/store/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@ const config = {
},
getters: {
config (state) {
console.log("config: ", state)
return state.config
}
},
actions: {
[FETCH_CONFIG] ({ commit }, params) {
async [FETCH_CONFIG] ({ commit }, params) {
// We only fetch config if we don't have a copy cached
if (this.getters.config === null) {
return new Promise((resolve, reject) => {
ApiService.query('config', params)
.then((response) => {
commit(SET_CONFIG, response.data)
})
.catch((error) => {
reject(error)
})
})
if (getters.config === null) {
try {
const response = await ApiService.query('config', params)
console.log("config response", response)
commit(SET_CONFIG, response.data)
} catch (error) {
console.log("Error: ", error)
}
}
}
}
Expand Down
24 changes: 11 additions & 13 deletions app/frontend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ import Surveys from '@/surveys/views/Surveys.vue'
// QaQc
import QaQcDashboard from '@/qaqc/views/QaQcDashboard.vue'

const Test = {
template: '<div style={{borderColor: "red";}}>Test!</div>'
}

// Vue.use(Router)
const routes = [
// aquifers routes
Expand Down Expand Up @@ -269,7 +265,9 @@ const router = createRouter({
}
});

const isAuthenticated = () => router.app?.$keycloak?.authenticated ?? false;
const isAuthenticated = () => {
return router.app?.$keycloak?.authenticated ?? false;
}

const authenciateUser = (next) => {
authenticate.authenticate(store).then((keycloak) => {
Expand All @@ -283,12 +281,12 @@ const authenciateUser = (next) => {
})
}

// router.beforeEach((to, from, next) => {
// if (!isAuthenticated) {
// authenciateUser(next)
// } else {
// next()
// }
// })
router.beforeEach((to, from, next) => {
if (!isAuthenticated()) {
authenciateUser(next)
} else {
next()
}
})

export default router
export default router

0 comments on commit 047a34f

Please sign in to comment.