forked from IBM/cloud-native-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·92 lines (83 loc) · 2.63 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Vue from 'vue'
import App from './App.vue'
import store from './store'
import router from './router';
import BootstrapVue from 'bootstrap-vue';
import Keycloak from 'keycloak-js';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap-vue/dist/bootstrap-vue.css';
Vue.config.productionTip = false
Vue.config.devtools = true
Vue.use(BootstrapVue);
let currentHostname = window.location.hostname;
let urls;
if (currentHostname.indexOf('localhost') > -1) {
urls = {
api: 'http://localhost:8081/',
login: 'https://YOUR-URL/auth' // insert your http or https://<KeycloakURL>/auth
}
store.commit("setAPIAndLogin", urls);
}
else {
let webapiUrl = 'https://' + currentHostname + '/'; // OpenShift part 1 using http
let keycloakUrl = 'https://' + currentHostname + '/auth'; // OpenShift part 1 using http
urls = {
api: webapiUrl,
login: keycloakUrl
}
store.commit("setAPIAndLogin", urls);
}
let initOptions = {
url: store.state.endpoints.login , realm: 'quarkus', clientId: 'frontend', onLoad: 'login-required'
}
let keycloak = Keycloak(initOptions);
keycloak.init({ onLoad: initOptions.onLoad }).then((auth) => {
if (!auth) {
window.location.reload();
}
new Vue({
store,
router,
render: h => h(App)
}).$mount('#app')
let payload = {
idToken: keycloak.idToken,
accessToken: keycloak.token
}
if (keycloak.token && keycloak.idToken && keycloak.token != '' && keycloak.idToken != '') {
store.commit("login", payload);
console.log("--> log: get username : " + keycloak.subject);
console.log("--> log: User has logged in: " + keycloak.subject);
console.log("--> log: TokenParsed: "+ JSON.stringify(keycloak.tokenParsed));
console.log("--> log: User name: " + keycloak.tokenParsed.preferred_username);
console.log("--> log: accessToken: " + store.state.user.accessToken);
payload = {
name: keycloak.tokenParsed.preferred_username
};
store.commit("setName", payload);
}
else {
store.commit("logout");
}
setInterval(() => {
keycloak.updateToken(70).then((refreshed) => {
if (refreshed) {
console.log("--> log: updateToken");
let payloadRefreshedTokens = {
idToken: keycloak.idToken,
accessToken: keycloak.token
}
if (keycloak.token && keycloak.idToken && keycloak.token != '' && keycloak.idToken != '') {
store.commit("login", payloadRefreshedTokens);
}
else {
store.commit("logout");
}
}
}).catch(() => {
console.log('-->log: Failed to refresh token');
});
}, 60000)
}).catch(() => {
console.log("-->log: Failed to authenticate");
});