Skip to content

Commit

Permalink
Routing working for authenticated routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
fergmac committed Jul 19, 2024
1 parent 047a34f commit 88c92ba
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app/frontend/src/common/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default {
token,
refreshToken,
idToken }
).success((result) => {
).then((result) => {
if (instance.authenticated) {
// We may have been authenticated, but the token could be expired.
instance.updateToken(60).success(() => {
Expand All @@ -156,7 +156,7 @@ export default {
store.commit('SET_KEYCLOAK', instance)
resolve(instance)
}
}).error((e) => {
}).catch((e) => {
reject(e)
})
}
Expand Down
4 changes: 0 additions & 4 deletions app/frontend/src/common/components/Auth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ export default {
keyCloakLogin () {
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 @@ -53,7 +51,6 @@ export default {
}
}
}).catch((error) => {
console.error("Keycloak login failed", error);
this.$store.commit(SET_ERROR, { error: 'Cannot contact SSO provider' });
});
},
Expand All @@ -72,7 +69,6 @@ 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: 0 additions & 1 deletion app/frontend/src/common/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export default {
computed: {
...mapGetters(['userRoles', 'config']),
hasConfig () {
console.log("has config: ", Boolean(this.config))
return Boolean(this.config)
},
getEnvironmentMessage () {
Expand Down
4 changes: 1 addition & 3 deletions app/frontend/src/common/store/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ const config = {
},
getters: {
config (state) {
console.log("config: ", state)
return state.config
}
},
actions: {
async [FETCH_CONFIG] ({ commit }, params) {
async [FETCH_CONFIG] ({ state, getters, commit }, params) {
// We only fetch config if we don't have a copy cached
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
8 changes: 4 additions & 4 deletions app/frontend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ app.use(store);

app.mixin({
methods: {
...store.dispatch,
...mapActions([FETCH_CONFIG])
},
created() {
// this.FETCH_CONFIG(); // Ensure FETCH_CONFIG is correctly defined in actions
// window._paq.push(["trackPageView"]); // Example of tracking pageview
},
this.FETCH_CONFIG()
// window._paq.push(["trackPageView"]);
}
});

app.mount('#app');
23 changes: 19 additions & 4 deletions app/frontend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ const router = createRouter({
});

const isAuthenticated = () => {
return router.app?.$keycloak?.authenticated ?? false;
if (!router?.app || !router?.app?.$keycloack) return false;

return router.app.$keycloak;
}

const authenciateUser = (next) => {
Expand All @@ -281,9 +283,22 @@ const authenciateUser = (next) => {
})
}

router.beforeEach((to, from, next) => {
if (!isAuthenticated()) {
authenciateUser(next)
router.beforeEach(async (to, from, next) => {
if (!router?.app?.$keycloack) {
try {
const isAuthenticated = await authenticate.authenticate(store);

console.log("isAuthenticated: ", isAuthenticated);

if (keycloak?.authenticated) {
Sentry.setUser({ username: keycloak.tokenParsed.preferred_username })
}
next()
} catch (error) {
console.log("Keycloak Authentication ERROR: ", error);
next()
}

} else {
next()
}
Expand Down

0 comments on commit 88c92ba

Please sign in to comment.