@alfabit/keycloak 0.0.41 → 0.0.43

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.41",
4
+ "version": "0.0.43",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
7
7
  "main": "src/index.ts",
@@ -20,8 +20,8 @@
20
20
  "keycloak-js": "^26.2.0"
21
21
  },
22
22
  "devDependencies": {
23
- "@alfabit/constants": "^0.0.21",
24
23
  "@alfabit/analitics": "^0.0.18",
24
+ "@alfabit/constants": "^0.0.21",
25
25
  "@intlify/unplugin-vue-i18n": "^6.0.8",
26
26
  "@tanstack/vue-query": "^5.72.0",
27
27
  "@types/lodash-es": "^4.17.12",
@@ -9,11 +9,11 @@ export const postWebLogin = async () => {
9
9
  const headers: Record<string, string> = {
10
10
  "Content-Type": "application/json",
11
11
  };
12
- const token = getToken();
13
- if(token) headers['Authorization'] = `Bearer ${token}`;
12
+ const token = getToken();
13
+ if (token) headers['Authorization'] = `Bearer ${token}`;
14
14
 
15
15
  let url = `${WALLET_API_HOST}/public/web/login/`;
16
-
16
+
17
17
  try {
18
18
  const analytics = useAnalytics();
19
19
  const clientID = await analytics.getClientID();
@@ -41,4 +41,31 @@ export const postWebLogin = async () => {
41
41
  const data = await res.json();
42
42
  if (referral_token) localStorage.removeItem(REF_KEY);
43
43
  return data;
44
+ };
45
+
46
+
47
+ export const getPublicProfile = async () => {
48
+ if (typeof window === 'undefined') return;
49
+
50
+ const headers: Record<string, string> = {
51
+ "Content-Type": "application/json",
52
+ };
53
+ const token = getToken();
54
+ if (token) headers['Authorization'] = `Bearer ${token}`;
55
+
56
+ let url = `${WALLET_API_HOST}/public/getProfile`;
57
+
58
+ const params: RequestInit = {
59
+ method: "GET",
60
+ headers,
61
+ }
62
+
63
+ const res = await fetch(url, params);
64
+
65
+ if (!res.ok) {
66
+ throw new Error(`postWebLogin /public/web/login/: HTTP error: ${res.status}`);
67
+ }
68
+
69
+ const data = await res.json();
70
+ return data;
44
71
  };
@@ -1,10 +1,52 @@
1
1
  import Keycloak, { type KeycloakInitOptions } from 'keycloak-js';
2
2
  import { inject, reactive, readonly, type Ref, ref } from 'vue';
3
- import { postWebLogin } from '../api/wallet/endpoints';
3
+ import { postWebLogin, getPublicProfile } from '../api/wallet/endpoints';
4
4
  import { AppNameEnum, APP_NAME, IS_TELEGRAM_MINI_APP, KEYCLOAK_LOGIN_REQUIRED, OPENID_CLIENT_ID, OPENID_REALM, OPENID_URL, LocationEnum, IS_LOCALHOST } from '@alfabit/constants';
5
5
 
6
6
  export type TIdpHint = 'email' | 'google' | 'apple';
7
7
 
8
+ enum TariffsGridTypeEnum {
9
+ Deposit = 'deposit',
10
+ Withdraw = 'withdraw',
11
+ Invoice = 'invoice',
12
+ Hedging = 'hedging',
13
+ FiatExchange = 'fiat_exchange',
14
+ InternalTransfer = 'internal_transfer',
15
+ Orders = 'orders',
16
+ }
17
+
18
+ interface TariffGrid {
19
+ tariff_name?: string | null;
20
+ tariff_type?: TariffsGridTypeEnum | null;
21
+ commission?: string | null;
22
+ trading_volume?: number | null;
23
+ is_kyc_required?: boolean | null;
24
+ id?: string;
25
+ created_date?: string;
26
+ creator_id?: number | null;
27
+ modification_date?: string | null;
28
+ modifier_id?: number | null;
29
+ }
30
+
31
+ interface ProfileResponse {
32
+ telegram_id?: string | null;
33
+ telegram_username?: string | null;
34
+ email?: string | null;
35
+ created_at: number;
36
+ is_active?: boolean | null;
37
+ balance_usdt?: string | null;
38
+ bonus_balance_usdt?: string | null;
39
+ kyc_status?: string | null;
40
+ kyc_link?: string | null;
41
+ is_wallet_commission?: boolean;
42
+ is_need_withdraw_kyc?: boolean;
43
+ tariff_grid?: TariffGrid | null;
44
+ tariff_grid_current?: TariffGrid | null;
45
+ is_partner_exchange_limit_free?: boolean | null;
46
+ last_login?: string | null;
47
+ is_tariff_autocalculate?: boolean | null;
48
+ }
49
+
8
50
  export interface IUseKeycloak {
9
51
  login(email?: string, redirectUri?: string): void;
10
52
  loginWithGoogle(redirectUri?: string): void;
@@ -19,6 +61,7 @@ export interface IUseKeycloak {
19
61
 
20
62
  readonly isAuth: Ref<boolean>;
21
63
  readonly checkAuth: Ref<boolean>;
64
+ readonly userData: Ref<ProfileResponse | null>;
22
65
  readonly keycloak: Ref<Keycloak | null>;
23
66
  readonly isInitialized: Ref<boolean>;
24
67
  readonly keycloakUserData: Ref<Keycloak.KeycloakTokenParsed | null>;
@@ -32,27 +75,28 @@ const keycloak = ref<Keycloak | null>(null);
32
75
 
33
76
  const isAuth = ref(false);
34
77
  const checkAuth = ref(false);
78
+ const userData = ref<ProfileResponse | null>(null);
35
79
  const keycloakUserData = ref<Keycloak.KeycloakTokenParsed | null>(null);
36
80
  const locale = ref('en');
37
81
 
38
82
  const dontChangeRedirectUri = ref(false);
39
83
  const keycloakRedirectUriIsLogout = ref<string | undefined>(undefined);
40
84
 
41
- export const setKeycloakLocale = (newLocale: LocationEnum) => {
85
+ export function setKeycloakLocale(newLocale: LocationEnum) {
42
86
  locale.value = newLocale;
43
87
  };
44
88
 
45
- export const setDontChangeRedirectUri = (newDontChangeRedirectUri: boolean) => {
89
+ export function setDontChangeRedirectUri(newDontChangeRedirectUri: boolean) {
46
90
  dontChangeRedirectUri.value = newDontChangeRedirectUri;
47
91
  };
48
92
 
49
- export const setKeycloakRedirectUriIsLogout = (newKeycloakRedirectUriIsLogout: string | undefined) => {
93
+ export function setKeycloakRedirectUriIsLogout(newKeycloakRedirectUriIsLogout: string | undefined) {
50
94
  keycloakRedirectUriIsLogout.value = newKeycloakRedirectUriIsLogout;
51
95
  };
52
96
 
53
97
  const isInitialized = ref(false);
54
98
 
55
- const getUrl = (): string => {
99
+ function getUrl(): string {
56
100
  const url = new URL(window.location.href);
57
101
  const searchParams = new URLSearchParams(window.location.search);
58
102
 
@@ -94,8 +138,8 @@ function isMobile() {
94
138
  return /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
95
139
  }
96
140
 
97
- export const getAuthMethods = () => {
98
- const methods = reactive({
141
+ export function getAuthMethods() {
142
+ const methods = {
99
143
  async loginPopup(idpHint: TIdpHint, redirectUri?: string) {
100
144
  redirectUri = redirectUri ?? getUrl();
101
145
  if (isMobile()) {
@@ -245,7 +289,7 @@ export const getAuthMethods = () => {
245
289
  console.warn('Keycloak not defined! #configureOtp');
246
290
  }
247
291
  },
248
- });
292
+ };
249
293
  return methods;
250
294
  };
251
295
 
@@ -270,9 +314,11 @@ function clearTokens() {
270
314
  sessionStorage.removeItem(ID_TOKEN_NAME);
271
315
  }
272
316
 
273
- export const getToken = () => (IS_TELEGRAM_MINI_APP && !IS_LOCALHOST ? sessionStorage.getItem(TOKEN_NAME) : localStorage.getItem(TOKEN_NAME)) ?? undefined;
317
+ export function getToken() {
318
+ return (IS_TELEGRAM_MINI_APP && !IS_LOCALHOST ? sessionStorage.getItem(TOKEN_NAME) : localStorage.getItem(TOKEN_NAME)) ?? undefined;
319
+ }
274
320
 
275
- const parseJwt = (token: unknown) => {
321
+ function parseJwt(token: unknown) {
276
322
  if (typeof token !== 'string') throw new Error('Invalid JWT format: #1');
277
323
  const [headerB64, payloadB64] = token.split('.');
278
324
  if (!headerB64 || !payloadB64) throw new Error('Invalid JWT format: #2');
@@ -289,7 +335,7 @@ const parseJwt = (token: unknown) => {
289
335
  };
290
336
  };
291
337
 
292
- export const isAtExp = () => {
338
+ export function isAtExp() {
293
339
  if (typeof window === 'undefined') return false;
294
340
  try {
295
341
  return parseJwt(localStorage.getItem(TOKEN_NAME)).isExpired;
@@ -298,7 +344,7 @@ export const isAtExp = () => {
298
344
  }
299
345
  };
300
346
 
301
- export const isRtExp = () => {
347
+ export function isRtExp() {
302
348
  if (typeof window === 'undefined') return false;
303
349
  try {
304
350
  // если рт не истек, возвращаем true
@@ -414,7 +460,8 @@ export async function initKeycloak() {
414
460
  keycloakUserData.value = keycloak.value?.tokenParsed ?? null;
415
461
  isAuth.value = true;
416
462
  try {
417
- postWebLogin();
463
+ await postWebLogin();
464
+ userData.value = await getPublicProfile();
418
465
  } catch (e) { }
419
466
 
420
467
  //setTimeout(() => {
@@ -429,7 +476,8 @@ export async function initKeycloak() {
429
476
  keycloakUserData.value = keycloak.value?.tokenParsed ?? null;
430
477
  isAuth.value = true;
431
478
  try {
432
- postWebLogin();
479
+ await postWebLogin();
480
+ userData.value = await getPublicProfile();
433
481
  } catch { }
434
482
  }
435
483
  }
@@ -472,7 +520,7 @@ export async function createKeycloakInit({ keycloakInit }: IKeycliakInit = {}) {
472
520
 
473
521
  let _keycloakInit: IUseKeycloak | null = null;
474
522
 
475
- export const keycloakInit = (dontChangeRedirectUri?: Ref<boolean>, keycloakRedirectUriIsLogout?: Ref<string | undefined>): IUseKeycloak => {
523
+ export function keycloakInit(dontChangeRedirectUri?: Ref<boolean>, keycloakRedirectUriIsLogout?: Ref<string | undefined>): IUseKeycloak {
476
524
  if (_keycloakInit) return _keycloakInit;
477
525
 
478
526
  dontChangeRedirectUri ||= inject('dontChangeRedirectUri', ref(false));
@@ -483,6 +531,7 @@ export const keycloakInit = (dontChangeRedirectUri?: Ref<boolean>, keycloakRedir
483
531
  isInitialized: readonly(isInitialized),
484
532
  isAuth: readonly(isAuth),
485
533
  checkAuth: readonly(checkAuth),
534
+ userData: readonly(userData),
486
535
 
487
536
  keycloakUserData,
488
537