-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeycloakExample.vue
127 lines (104 loc) · 3.25 KB
/
KeycloakExample.vue
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<template>
<div class="keycloak">
<h1>Keycloak vue3 example</h1>
<div v-if="profile"> Hello {{ (profile.firstName + profile.firstName).trim()||profile.username }} {{profile.lastName}}</div>
<div v-if="hasRole"> has role </div>
<br/>
<button @click="see_token">see token</button>
<button @click="see_profile2">see profile</button>
<button @click="fetchData">Fetch Data</button>
<br />
<div v-if="loading">loading</div>
<div v-if="data">result: <b><pre>{{ data }}</pre></b></div>
<div v-if="error">error: <b><pre>{{ error }}</pre></b></div>
<div v-if="token">token: <textarea cols=60 rows=16 onclick="this.select()" >{{ token }}</textarea></div>
<br />
<button @click="revoke">revoke</button>
<button @click="logout">logout</button>
<button @click="revokeLogout">revoke Logout</button>
<button @click="account">account</button>
</div>
</template>
<script>
import {authFetchText,authFetchJSON,authFetch, stopTokenRefreshTimer} from 'keycloak-js-util'
export default {
data() {
return {
data: this.data||null,
token: this.token||null,
error: this.error||null,
loading: this.loading||null,
hasRole: this.hasRole||null,
profile: this.profile||null,
};
},
async mounted() {
await this.load_profile()
await this.load_check_role()
},
methods: {
async fetchData() {
try {
this.error=false
this.loading=true
this.data = await authFetchText('http://localhost:8082/v1/test')
// this.data = await authFetchJSON('http://localhost:8082/v1/test')
// const result = await authFetch('http://localhost:8082/v1/test')
// this.data = await result.json()
this.token = '';
} catch(e){
this.error=e.message
}
finally {
this.loading=false
}
},
async revoke() {
this.token = '';
try {
this.error=false
this.loading=true
this.data = await authFetchText('http://localhost:8082/v1/jwt-revoke')
} catch(e){
this.error=e.message
}
finally {
this.loading=false
}
},
logout() {
window.keycloak.logout();
},
async revokeLogout() {
stopTokenRefreshTimer();
await authFetchText('http://localhost:8082/v1/jwt-revoke')
window.keycloak.logout();
},
account() {
window.keycloak.accountManagement();
},
async see_profile2() {
const profile = await window.keycloak.loadUserProfile();
this.data = JSON.stringify( profile ,null,2); // make json formated for viewing.
this.token = '';
},
async see_token() {
this.data = '';
this.token = window.keycloak.token;
},
async load_profile() {
this.profile = await window.keycloak.loadUserProfile();
},
async load_check_role() {
this.hasRole = await keycloak.hasRealmRole('default-roles-keycloak-demo') // permissions by groups
// await keycloak.hasResourceRole(role, resource) // permissions role to resource
},
},
};
</script>
<style scoped>
.keycloak {margin-top:30px;}
.keycloak b {brackground-color:lightgray}
@media (min-width: 1024px) {
}
</style>