@alfabit/keycloak 0.0.8 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@alfabit/keycloak",
3
3
  "private": false,
4
- "version": "0.0.8",
4
+ "version": "0.0.10",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
7
7
  "main": "src/index.ts",
@@ -35,31 +35,36 @@ 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
 
48
- console.log('getUrl', {
49
- dontChangeRedirectUri,
50
- keycloakRedirectUriIsLogout,
51
- redirect_uri: searchParams.get('redirect_uri'),
52
- });
53
-
54
59
  if (searchParams.has('redirect_uri')) {
55
60
  const redirectUri = searchParams.get('redirect_uri');
56
61
  if (!!redirectUri) return redirectUri;
57
62
  }
58
63
 
59
64
  // если в роуте в meta есть параметр keycloakRedirectUriIsLogout - возвращаем его
60
- if (!!keycloakRedirectUriIsLogout && typeof keycloakRedirectUriIsLogout === 'string') return keycloakRedirectUriIsLogout;
65
+ if (!!keycloakRedirectUriIsLogout.value && typeof keycloakRedirectUriIsLogout.value === 'string') return `${url.origin}/${locale.value}/${keycloakRedirectUriIsLogout.value}`;
61
66
 
62
- // если в $route.meta (который передаем из родительского приложения) есть параметр dontChangeRedirectUri - то из адреса ну нужно удалять или добавлять /user
67
+ // если в $route.meta (который передаем из родительского приложения) есть параметр dontChangeRedirectUri - то из адреса НЕ нужно удалять или добавлять /user
63
68
  // если нет - то ничего не делать
64
69
 
65
70
  // if (!dontChangeRedirectUri) {
@@ -85,19 +90,14 @@ const getUrl = (dontChangeRedirectUri = false, keycloakRedirectUriIsLogout: stri
85
90
  return url.toString();
86
91
  };
87
92
 
88
- interface IGetAuthMethodsParams {
89
- dontChangeRedirectUri?: Ref<boolean>;
90
- keycloakRedirectUriIsLogout?: Ref<string | undefined>;
91
- }
92
-
93
93
  function isMobile() {
94
94
  return /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
95
95
  }
96
96
 
97
- export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRedirectUriIsLogout = ref(undefined) }: IGetAuthMethodsParams) => {
97
+ export const getAuthMethods = () => {
98
98
  const methods = reactive({
99
99
  async loginPopup(idpHint: TIdpHint, redirectUri?: string) {
100
- redirectUri = redirectUri ?? getUrl(dontChangeRedirectUri.value, keycloakRedirectUriIsLogout.value);
100
+ redirectUri = redirectUri ?? getUrl();
101
101
 
102
102
  console.log({
103
103
  isMobile: isMobile(),
@@ -164,7 +164,7 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
164
164
  login(email?: string, redirectUri?: string) {
165
165
  if (keycloak.value) {
166
166
  keycloak.value.login({
167
- redirectUri: redirectUri ?? getUrl(dontChangeRedirectUri.value, keycloakRedirectUriIsLogout.value),
167
+ redirectUri: redirectUri ?? getUrl(),
168
168
  loginHint: email ?? '',
169
169
  locale: locale.value,
170
170
  });
@@ -176,9 +176,15 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
176
176
  if (keycloak.value) {
177
177
  clearTokens();
178
178
  setTimeout(() => {
179
+ console.log('logout', {
180
+ keycloakRedirectUriIsLogout: keycloakRedirectUriIsLogout.value,
181
+ dontChangeRedirectUri: dontChangeRedirectUri.value,
182
+ redirectUri: redirectUri ?? getUrl(),
183
+ });
184
+
179
185
  keycloak.value &&
180
186
  keycloak.value.logout({
181
- redirectUri: redirectUri ?? getUrl(dontChangeRedirectUri.value, keycloakRedirectUriIsLogout.value),
187
+ redirectUri: redirectUri ?? getUrl(),
182
188
  });
183
189
  }, 100);
184
190
  } else {
@@ -188,7 +194,7 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
188
194
  register(email?: string, redirectUri?: string) {
189
195
  if (keycloak.value) {
190
196
  keycloak.value.register({
191
- redirectUri: redirectUri ?? getUrl(dontChangeRedirectUri.value),
197
+ redirectUri: redirectUri ?? getUrl(),
192
198
  loginHint: email ?? '',
193
199
  locale: locale.value,
194
200
  });
@@ -201,7 +207,7 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
201
207
  keycloak.value
202
208
  ?.createLoginUrl({
203
209
  idpHint: 'google',
204
- redirectUri: redirectUri ?? getUrl(dontChangeRedirectUri.value),
210
+ redirectUri: redirectUri ?? getUrl(),
205
211
  })
206
212
  .then((loginUrl) => (window.location.href = `${loginUrl}&prompt=select_account`));
207
213
  } else {
@@ -211,7 +217,7 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
211
217
  loginWithApple(redirectUri?: string) {
212
218
  if (keycloak.value) {
213
219
  keycloak.value.login({
214
- redirectUri: redirectUri ?? getUrl(dontChangeRedirectUri.value),
220
+ redirectUri: redirectUri ?? getUrl(),
215
221
  idpHint: 'apple',
216
222
  locale: locale.value,
217
223
  });
@@ -222,7 +228,7 @@ export const getAuthMethods = ({ dontChangeRedirectUri = ref(false), keycloakRed
222
228
  loginWithTelegram(redirectUri?: string) {
223
229
  if (keycloak.value) {
224
230
  keycloak.value.login({
225
- redirectUri: redirectUri ?? getUrl(dontChangeRedirectUri.value),
231
+ redirectUri: redirectUri ?? getUrl(),
226
232
  idpHint: 'telegram',
227
233
  locale: locale.value,
228
234
  });
@@ -347,6 +353,10 @@ export async function initKeycloak() {
347
353
  clientId: OPENID_CLIENT_ID,
348
354
  });
349
355
 
356
+ console.log({
357
+ keycloak: keycloak.value,
358
+ });
359
+
350
360
  const isNeedAuth = !!getToken() && !isRtExp();
351
361
 
352
362
  // window.opener - если находимся в popup (но не в passport), то 100% уже авторизованы и можно поставить login-required
@@ -460,7 +470,7 @@ export const keycloakInit = (dontChangeRedirectUri?: Ref<boolean>, keycloakRedir
460
470
 
461
471
  keycloakUserData,
462
472
 
463
- ...getAuthMethods({ dontChangeRedirectUri, keycloakRedirectUriIsLogout }),
473
+ ...getAuthMethods(),
464
474
  getToken,
465
475
  isAtExp,
466
476
  isRtExp,
@@ -1,37 +1,22 @@
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 = () => {
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 = (options: KeycloakPluginOptions = {}) => {
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(_: App, options: KeycloakPluginOptions = {}) {
34
- createKeycloak(options);
18
+ install() {
19
+ createKeycloak();
35
20
  //app.config.globalProperties.$keycloak = useKeycloak();
36
21
  },
37
22
  };