@alfabit/keycloak 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/composables/use-keycloak.ts +24 -21
- package/src/plugins/keycloak.ts +5 -20
package/package.json
CHANGED
|
@@ -35,13 +35,24 @@ const checkAuth = ref(false);
|
|
|
35
35
|
const keycloakUserData = ref<Keycloak.KeycloakTokenParsed | null>(null);
|
|
36
36
|
const locale = ref('en');
|
|
37
37
|
|
|
38
|
+
const dontChangeRedirectUri = ref(false);
|
|
39
|
+
const keycloakRedirectUriIsLogout = ref<string | undefined>(undefined);
|
|
40
|
+
|
|
38
41
|
export const setKeycloakLocale = (newLocale: LocationEnum) => {
|
|
39
42
|
locale.value = newLocale;
|
|
40
43
|
};
|
|
41
44
|
|
|
45
|
+
export const setDontChangeRedirectUri = (newDontChangeRedirectUri: boolean) => {
|
|
46
|
+
dontChangeRedirectUri.value = newDontChangeRedirectUri;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const setKeycloakRedirectUriIsLogout = (newKeycloakRedirectUriIsLogout: string | undefined) => {
|
|
50
|
+
keycloakRedirectUriIsLogout.value = newKeycloakRedirectUriIsLogout;
|
|
51
|
+
};
|
|
52
|
+
|
|
42
53
|
const isInitialized = ref(false);
|
|
43
54
|
|
|
44
|
-
const getUrl = (
|
|
55
|
+
const getUrl = (): string => {
|
|
45
56
|
const url = new URL(window.location.href);
|
|
46
57
|
const searchParams = new URLSearchParams(window.location.search);
|
|
47
58
|
|
|
@@ -51,12 +62,9 @@ const getUrl = (dontChangeRedirectUri = false, keycloakRedirectUriIsLogout: stri
|
|
|
51
62
|
}
|
|
52
63
|
|
|
53
64
|
// если в роуте в meta есть параметр keycloakRedirectUriIsLogout - возвращаем его
|
|
54
|
-
if (!!keycloakRedirectUriIsLogout && typeof keycloakRedirectUriIsLogout === 'string') return `${locale.value}/${keycloakRedirectUriIsLogout}`;
|
|
65
|
+
if (!!keycloakRedirectUriIsLogout.value && typeof keycloakRedirectUriIsLogout.value === 'string') return `${url.origin}/${locale.value}/${keycloakRedirectUriIsLogout.value}`;
|
|
55
66
|
|
|
56
|
-
|
|
57
|
-
dontChangeRedirectUri,
|
|
58
|
-
});
|
|
59
|
-
// если в $route.meta (который передаем из родительского приложения) есть параметр dontChangeRedirectUri - то из адреса ну нужно удалять или добавлять /user
|
|
67
|
+
// если в $route.meta (который передаем из родительского приложения) есть параметр dontChangeRedirectUri - то из адреса НЕ нужно удалять или добавлять /user
|
|
60
68
|
// если нет - то ничего не делать
|
|
61
69
|
|
|
62
70
|
// if (!dontChangeRedirectUri) {
|
|
@@ -82,19 +90,14 @@ const getUrl = (dontChangeRedirectUri = false, keycloakRedirectUriIsLogout: stri
|
|
|
82
90
|
return url.toString();
|
|
83
91
|
};
|
|
84
92
|
|
|
85
|
-
interface IGetAuthMethodsParams {
|
|
86
|
-
dontChangeRedirectUri?: Ref<boolean>;
|
|
87
|
-
keycloakRedirectUriIsLogout?: Ref<string | undefined>;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
93
|
function isMobile() {
|
|
91
94
|
return /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
|
|
92
95
|
}
|
|
93
96
|
|
|
94
|
-
export const getAuthMethods = (
|
|
97
|
+
export const getAuthMethods = () => {
|
|
95
98
|
const methods = reactive({
|
|
96
99
|
async loginPopup(idpHint: TIdpHint, redirectUri?: string) {
|
|
97
|
-
redirectUri = redirectUri ?? getUrl(
|
|
100
|
+
redirectUri = redirectUri ?? getUrl();
|
|
98
101
|
|
|
99
102
|
console.log({
|
|
100
103
|
isMobile: isMobile(),
|
|
@@ -161,7 +164,7 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
|
|
|
161
164
|
login(email?: string, redirectUri?: string) {
|
|
162
165
|
if (keycloak.value) {
|
|
163
166
|
keycloak.value.login({
|
|
164
|
-
redirectUri: redirectUri ?? getUrl(
|
|
167
|
+
redirectUri: redirectUri ?? getUrl(),
|
|
165
168
|
loginHint: email ?? '',
|
|
166
169
|
locale: locale.value,
|
|
167
170
|
});
|
|
@@ -176,12 +179,12 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
|
|
|
176
179
|
console.log('logout', {
|
|
177
180
|
keycloakRedirectUriIsLogout: keycloakRedirectUriIsLogout.value,
|
|
178
181
|
dontChangeRedirectUri: dontChangeRedirectUri.value,
|
|
179
|
-
redirectUri: redirectUri ?? getUrl(
|
|
182
|
+
redirectUri: redirectUri ?? getUrl(),
|
|
180
183
|
});
|
|
181
184
|
|
|
182
185
|
keycloak.value &&
|
|
183
186
|
keycloak.value.logout({
|
|
184
|
-
redirectUri: redirectUri ?? getUrl(
|
|
187
|
+
redirectUri: redirectUri ?? getUrl(),
|
|
185
188
|
});
|
|
186
189
|
}, 100);
|
|
187
190
|
} else {
|
|
@@ -191,7 +194,7 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
|
|
|
191
194
|
register(email?: string, redirectUri?: string) {
|
|
192
195
|
if (keycloak.value) {
|
|
193
196
|
keycloak.value.register({
|
|
194
|
-
redirectUri: redirectUri ?? getUrl(
|
|
197
|
+
redirectUri: redirectUri ?? getUrl(),
|
|
195
198
|
loginHint: email ?? '',
|
|
196
199
|
locale: locale.value,
|
|
197
200
|
});
|
|
@@ -204,7 +207,7 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
|
|
|
204
207
|
keycloak.value
|
|
205
208
|
?.createLoginUrl({
|
|
206
209
|
idpHint: 'google',
|
|
207
|
-
redirectUri: redirectUri ?? getUrl(
|
|
210
|
+
redirectUri: redirectUri ?? getUrl(),
|
|
208
211
|
})
|
|
209
212
|
.then((loginUrl) => (window.location.href = `${loginUrl}&prompt=select_account`));
|
|
210
213
|
} else {
|
|
@@ -214,7 +217,7 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
|
|
|
214
217
|
loginWithApple(redirectUri?: string) {
|
|
215
218
|
if (keycloak.value) {
|
|
216
219
|
keycloak.value.login({
|
|
217
|
-
redirectUri: redirectUri ?? getUrl(
|
|
220
|
+
redirectUri: redirectUri ?? getUrl(),
|
|
218
221
|
idpHint: 'apple',
|
|
219
222
|
locale: locale.value,
|
|
220
223
|
});
|
|
@@ -225,7 +228,7 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
|
|
|
225
228
|
loginWithTelegram(redirectUri?: string) {
|
|
226
229
|
if (keycloak.value) {
|
|
227
230
|
keycloak.value.login({
|
|
228
|
-
redirectUri: redirectUri ?? getUrl(
|
|
231
|
+
redirectUri: redirectUri ?? getUrl(),
|
|
229
232
|
idpHint: 'telegram',
|
|
230
233
|
locale: locale.value,
|
|
231
234
|
});
|
|
@@ -467,7 +470,7 @@ export const keycloakInit = (dontChangeRedirectUri?: Ref<boolean>, keycloakRedir
|
|
|
467
470
|
|
|
468
471
|
keycloakUserData,
|
|
469
472
|
|
|
470
|
-
...getAuthMethods(
|
|
473
|
+
...getAuthMethods(),
|
|
471
474
|
getToken,
|
|
472
475
|
isAtExp,
|
|
473
476
|
isRtExp,
|
package/src/plugins/keycloak.ts
CHANGED
|
@@ -1,37 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ref } from 'vue';
|
|
2
2
|
import { createKeycloakInit, getAuthMethods, keycloakInit } from '../composables';
|
|
3
3
|
|
|
4
|
-
interface KeycloakPluginOptions {
|
|
5
|
-
dontChangeRedirectUri?: Ref<boolean>;
|
|
6
|
-
keycloakRedirectUriIsLogout?: Ref<string | undefined>;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
let dontChangeRedirectUriInit = ref(false);
|
|
10
|
-
let keycloakRedirectUriIsLogoutInit = ref<string | undefined>(undefined);
|
|
11
|
-
|
|
12
4
|
export const useKeycloak = () => {
|
|
13
5
|
const keycloak = keycloakInit(ref(false), ref(undefined));
|
|
14
|
-
const auth = getAuthMethods(
|
|
15
|
-
dontChangeRedirectUri: dontChangeRedirectUriInit,
|
|
16
|
-
keycloakRedirectUriIsLogout: keycloakRedirectUriIsLogoutInit,
|
|
17
|
-
});
|
|
18
|
-
|
|
6
|
+
const auth = getAuthMethods();
|
|
19
7
|
return {
|
|
20
8
|
...keycloak,
|
|
21
9
|
...auth,
|
|
22
10
|
};
|
|
23
11
|
};
|
|
24
12
|
|
|
25
|
-
export const createKeycloak = (
|
|
26
|
-
const { dontChangeRedirectUri, keycloakRedirectUriIsLogout } = options;
|
|
27
|
-
dontChangeRedirectUriInit = computed(() => dontChangeRedirectUri?.value ?? false);
|
|
28
|
-
keycloakRedirectUriIsLogoutInit = computed(() => keycloakRedirectUriIsLogout?.value ?? undefined);
|
|
13
|
+
export const createKeycloak = () => {
|
|
29
14
|
createKeycloakInit();
|
|
30
15
|
};
|
|
31
16
|
|
|
32
17
|
export const keycloakPlugin = {
|
|
33
|
-
install(
|
|
34
|
-
createKeycloak(
|
|
18
|
+
install() {
|
|
19
|
+
createKeycloak();
|
|
35
20
|
//app.config.globalProperties.$keycloak = useKeycloak();
|
|
36
21
|
},
|
|
37
22
|
};
|