Skip to content

Commit 9bda857

Browse files
committed
Debugging keycloack
1 parent dde92b7 commit 9bda857

File tree

2 files changed

+82
-57
lines changed

2 files changed

+82
-57
lines changed

app/frontend/src/common/authenticate.js

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ export default {
8686
renewToken (instance, retries = 0) {
8787
const maxRetries = 2
8888

89-
instance.updateToken(1800).success((refreshed) => {
89+
instance.updateToken(1800).then((refreshed) => {
9090
if (refreshed) {
9191
this.setLocalToken(instance)
9292
}
9393
this.scheduleRenewal(instance)
94-
}).error((e) => {
94+
}).catch((e) => {
9595
console.log(e)
9696
// The refresh token is expired or was rejected
9797
// we will retry after 60 sec (up to the count defined by maxRetries)
@@ -113,52 +113,61 @@ export default {
113113
return new Promise((resolve, reject) => {
114114
this.getInstance()
115115
.then((instance) => {
116+
console.log("INSTANCE: ", instance)
116117
if (instance.authenticated && ApiService.hasAuthHeader() && !instance.isTokenExpired(0)) {
117118
// We've already authenticated, have a header, and we've not expired.
118119
resolve(instance)
119120
} else {
121+
122+
this.removeLocalToken()
123+
instance.clearToken()
124+
// We update the store reference only after wiring up the API. (Someone might be waiting
125+
// for login to complete before taking some action. )
126+
store.commit('SET_KEYCLOAK', instance)
127+
resolve(instance)
120128
// Attempt to retrieve a stored token, this may avoid us having to refresh the page.
121-
const token = localStorage.getItem('token')
122-
const refreshToken = localStorage.getItem('refreshToken')
123-
const idToken = localStorage.getItem('idToken')
124-
instance.init({
125-
pkceMethod: 'S256',
126-
onLoad: 'check-sso',
127-
checkLoginIframe: true,
128-
timeSkew: 10, // Allow for some deviation
129-
token,
130-
refreshToken,
131-
idToken }
132-
).success((result) => {
133-
if (instance.authenticated) {
134-
// We may have been authenticated, but the token could be expired.
135-
instance.updateToken(60).success(() => {
136-
// Store the token to avoid future round trips, and wire up the API
137-
this.setLocalToken(instance)
138-
// We update the store reference only after wiring up the API. (Someone might be waiting
139-
// for login to complete before taking some action. )
140-
// Assumes that store passed in includes a 'SET_KEYCLOAK' mutation!
141-
store.commit('SET_KEYCLOAK', instance)
142-
this.scheduleRenewal(instance)
143-
resolve(instance)
144-
}).error(() => {
145-
// The refresh token is expired or was rejected
146-
this.removeLocalToken()
147-
instance.clearToken()
148-
// We update the store reference only after wiring up the API. (Someone might be waiting
149-
// for login to complete before taking some action. )
150-
store.commit('SET_KEYCLOAK', instance)
151-
resolve(instance)
152-
})
153-
} else {
154-
// We may have failed to authenticate, for many reasons, e.g. - it may be we never logged in,
155-
// or have an expired token.
156-
store.commit('SET_KEYCLOAK', instance)
157-
resolve(instance)
158-
}
159-
}).error((e) => {
160-
reject(e)
161-
})
129+
// const token = localStorage.getItem('token')
130+
// const refreshToken = localStorage.getItem('refreshToken')
131+
// const idToken = localStorage.getItem('idToken')
132+
// instance.init({
133+
// pkceMethod: 'S256',
134+
// onLoad: 'check-sso',
135+
// checkLoginIframe: true,
136+
// timeSkew: 10, // Allow for some deviation
137+
// token,
138+
// refreshToken,
139+
// idToken,
140+
// }
141+
// ).then((result) => {
142+
// if (instance.authenticated) {
143+
// // We may have been authenticated, but the token could be expired.
144+
// instance.updateToken(60).then(() => {
145+
// // Store the token to avoid future round trips, and wire up the API
146+
// this.setLocalToken(instance)
147+
// // We update the store reference only after wiring up the API. (Someone might be waiting
148+
// // for login to complete before taking some action. )
149+
// // Assumes that store passed in includes a 'SET_KEYCLOAK' mutation!
150+
// store.commit('SET_KEYCLOAK', instance)
151+
// this.scheduleRenewal(instance)
152+
// resolve(instance)
153+
// }).error(() => {
154+
// // The refresh token is expired or was rejected
155+
// this.removeLocalToken()
156+
// instance.clearToken()
157+
// // We update the store reference only after wiring up the API. (Someone might be waiting
158+
// // for login to complete before taking some action. )
159+
// store.commit('SET_KEYCLOAK', instance)
160+
// resolve(instance)
161+
// })
162+
// } else {
163+
// // We may have failed to authenticate, for many reasons, e.g. - it may be we never logged in,
164+
// // or have an expired token.
165+
// store.commit('SET_KEYCLOAK', instance)
166+
// resolve(instance)
167+
// }
168+
// }).then((e) => {
169+
// reject(e)
170+
// })
162171
}
163172
})
164173
.catch((error) => {

app/frontend/src/common/components/Auth.vue

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,37 @@ export default {
3737
this.keyCloakLogin()
3838
}
3939
},
40-
keyCloakLogin () {
41-
this.keycloak.init().success(() => {
42-
this.keycloak.login({ idpHint: this.config.sso_idp_hint }).success((authenticated) => {
43-
if (authenticated) {
44-
ApiService.authHeader('JWT', this.keycloak.token)
45-
if (window.localStorage) {
46-
localStorage.setItem('token', this.keycloak.token)
47-
localStorage.setItem('refreshToken', this.keycloak.refreshToken)
48-
localStorage.setItem('idToken', this.keycloak.idToken)
49-
}
50-
}
51-
}).error((e) => {
52-
this.$store.commit(SET_ERROR, { error: 'Cannot contact SSO provider' })
40+
async keyCloakLogin () {
41+
42+
try {
43+
const authenticated = await this.keycloak.init({
44+
flow: 'standard'
5345
})
54-
})
46+
console.log("Authenticated: ", authenticated)
47+
} catch(error) {
48+
console.error("Failed to initialize Keycloak: ", error);
49+
}
50+
51+
// console.log("before init")
52+
// this.keycloak.init({
53+
// adapter: 'default',
54+
// flow: 'implicit'
55+
// }).then(() => {
56+
// console.log("keyCloakLogin .then()")
57+
// this.keycloak.login({ idpHint: this.config.sso_idp_hint }).then((authenticated) => {
58+
// if (authenticated) {
59+
// ApiService.authHeader('JWT', this.keycloak.token)
60+
// if (window.localStorage) {
61+
// localStorage.setItem('token', this.keycloak.token)
62+
// localStorage.setItem('refreshToken', this.keycloak.refreshToken)
63+
// localStorage.setItem('idToken', this.keycloak.idToken)
64+
// }
65+
// }
66+
// }).catch((e) => {
67+
// console.log("keyCloakLogin ERROR: ", e)
68+
// this.$store.commit(SET_ERROR, { error: 'Cannot contact SSO provider' })
69+
// })
70+
// })
5571
},
5672
keyCloakLogout () {
5773
// This should log the user out, but unfortunately does not delete the cookie storing the user

0 commit comments

Comments
 (0)