@alfabit/keycloak 0.0.9 → 0.0.11

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@alfabit/keycloak",
3
3
  "private": false,
4
- "version": "0.0.9",
4
+ "version": "0.0.11",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
7
7
  "main": "src/index.ts",
@@ -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 = (dontChangeRedirectUri = false, keycloakRedirectUriIsLogout: string | undefined = undefined): string => {
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
- console.log({
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 = ({ dontChangeRedirectUri = ref(false), keycloakRedirectUriIsLogout = ref(undefined) }: IGetAuthMethodsParams) => {
97
+ export const getAuthMethods = () => {
95
98
  const methods = reactive({
96
99
  async loginPopup(idpHint: TIdpHint, redirectUri?: string) {
97
- redirectUri = redirectUri ?? getUrl(dontChangeRedirectUri.value, keycloakRedirectUriIsLogout.value);
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(dontChangeRedirectUri.value, keycloakRedirectUriIsLogout.value),
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(dontChangeRedirectUri.value, keycloakRedirectUriIsLogout.value),
182
+ redirectUri: redirectUri ?? getUrl(),
180
183
  });
181
184
 
182
185
  keycloak.value &&
183
186
  keycloak.value.logout({
184
- redirectUri: redirectUri ?? getUrl(dontChangeRedirectUri.value, keycloakRedirectUriIsLogout.value),
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(dontChangeRedirectUri.value),
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(dontChangeRedirectUri.value),
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(dontChangeRedirectUri.value),
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(dontChangeRedirectUri.value),
231
+ redirectUri: redirectUri ?? getUrl(),
229
232
  idpHint: 'telegram',
230
233
  locale: locale.value,
231
234
  });
@@ -467,11 +470,13 @@ export const keycloakInit = (dontChangeRedirectUri?: Ref<boolean>, keycloakRedir
467
470
 
468
471
  keycloakUserData,
469
472
 
470
- ...getAuthMethods({ dontChangeRedirectUri, keycloakRedirectUriIsLogout }),
473
+ ...getAuthMethods(),
471
474
  getToken,
472
475
  isAtExp,
473
476
  isRtExp,
474
477
  };
475
478
 
479
+ console.log('@alfabit/keycloak fn keycloakInit', {_keycloakInit});
480
+
476
481
  return _keycloakInit;
477
482
  };
@@ -1,37 +1,25 @@
1
- import { computed, ref, type App, type Ref } from 'vue';
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 = () => {
5
+ console.log('@alfabit/keycloak fn useKeycloak');
13
6
  const keycloak = keycloakInit(ref(false), ref(undefined));
14
- const auth = getAuthMethods({
15
- dontChangeRedirectUri: dontChangeRedirectUriInit,
16
- keycloakRedirectUriIsLogout: keycloakRedirectUriIsLogoutInit,
17
- });
18
-
7
+ const auth = getAuthMethods();
19
8
  return {
20
9
  ...keycloak,
21
10
  ...auth,
22
11
  };
23
12
  };
24
13
 
25
- export const createKeycloak = (options: KeycloakPluginOptions = {}) => {
26
- const { dontChangeRedirectUri, keycloakRedirectUriIsLogout } = options;
27
- dontChangeRedirectUriInit = computed(() => dontChangeRedirectUri?.value ?? false);
28
- keycloakRedirectUriIsLogoutInit = computed(() => keycloakRedirectUriIsLogout?.value ?? undefined);
14
+ export const createKeycloak = () => {
15
+ console.log('@alfabit/keycloak fn createKeycloak');
29
16
  createKeycloakInit();
30
17
  };
31
18
 
32
19
  export const keycloakPlugin = {
33
- install(_: App, options: KeycloakPluginOptions = {}) {
34
- createKeycloak(options);
20
+ install() {
21
+ console.log('@alfabit/keycloak plugin install');
22
+ createKeycloak();
35
23
  //app.config.globalProperties.$keycloak = useKeycloak();
36
24
  },
37
25
  };