academe-kit 0.3.1 → 0.3.3

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/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import React$1 from 'react';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
- import { KeycloakLoginOptions } from 'keycloak-js';
3
+ import { KeycloakInitOptions, KeycloakLoginOptions } from 'keycloak-js';
4
4
  import * as openapi_fetch from 'openapi-fetch';
5
5
  import openapi_fetch__default from 'openapi-fetch';
6
6
  import { ClassValue } from 'clsx';
@@ -10185,15 +10185,18 @@ interface AcademeServices {
10185
10185
 
10186
10186
  type AcademeKeycloakContextProps = {
10187
10187
  realm: string;
10188
- hubUrl: string;
10188
+ hubUrl?: string;
10189
10189
  clientId: string;
10190
10190
  keycloakUrl: string;
10191
10191
  apiBaseUrl?: string;
10192
+ initOptions?: KeycloakInitOptions;
10193
+ skipApiUserFetch?: boolean;
10192
10194
  children: React.ReactElement;
10193
10195
  };
10194
10196
  type SecurityProviderProps = {
10195
- hubUrl: string;
10197
+ hubUrl?: string;
10196
10198
  apiBaseUrl?: string;
10199
+ skipApiUserFetch?: boolean;
10197
10200
  children: React.ReactElement;
10198
10201
  };
10199
10202
  type KeycloakUser = {
@@ -10216,6 +10219,7 @@ type SecurityContextType = {
10216
10219
  hasClientRole: (role: string, resource?: string) => boolean;
10217
10220
  apiClient: AcademeApiClient | null;
10218
10221
  services: AcademeServices | null;
10222
+ accessToken: string | undefined;
10219
10223
  };
10220
10224
 
10221
10225
  declare const AcademeAuthProvider: React.FC<AcademeKeycloakContextProps>;
@@ -10242,5 +10246,11 @@ declare enum BACKOFFICE_ROLES {
10242
10246
  declare enum DASHBOARD_ROLES {
10243
10247
  }
10244
10248
 
10245
- export { AcademeAuthProvider, BACKOFFICE_ROLES, Button, DASHBOARD_ROLES, GLOBAL_ROLES, ProtectedApp, ProtectedComponent, ProtectedRouter, Spinner, academeApi_d as apiTypes, cn, createAcademeApiClient, index_d as types, useAcademeAuth };
10249
+ declare enum APPLICATIONS_ROLES {
10250
+ ACCESS_NINA = "Acesso nina",
10251
+ ACCESS_MIKE = "Acesso mike",
10252
+ VIEW_WIDGET = "Visualizar Widget"
10253
+ }
10254
+
10255
+ export { APPLICATIONS_ROLES, AcademeAuthProvider, BACKOFFICE_ROLES, Button, DASHBOARD_ROLES, GLOBAL_ROLES, ProtectedApp, ProtectedComponent, ProtectedRouter, Spinner, academeApi_d as apiTypes, cn, createAcademeApiClient, index_d as types, useAcademeAuth };
10246
10256
  export type { AcademeApiClient, AcademeKeycloakContextProps, AcademeUser, ButtonProps, KeycloakUser, SecurityContextType, SecurityProviderProps };
package/dist/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
- import React__default, { createContext, useContext, useState, useMemo, useEffect, useCallback, forwardRef, createElement } from 'react';
3
+ import React__default, { createContext, useContext, useMemo, useState, useEffect, useCallback, forwardRef, createElement } from 'react';
4
4
 
5
5
  function r(e){var t,f,n="";if("string"==typeof e||"number"==typeof e)n+=e;else if("object"==typeof e)if(Array.isArray(e)){var o=e.length;for(t=0;t<o;t++)e[t]&&(f=r(e[t]))&&(n&&(n+=" "),n+=f);}else for(f in e)e[f]&&(n&&(n+=" "),n+=f);return n}function clsx(){for(var e,t,f=0,n="",o=arguments.length;f<o;f++)(e=arguments[f])&&(t=r(e))&&(n&&(n+=" "),n+=t);return n}
6
6
 
@@ -3259,8 +3259,23 @@ var GLOBAL_ROLES;
3259
3259
  GLOBAL_ROLES["GUARDIAN"] = "guardian";
3260
3260
  })(GLOBAL_ROLES || (GLOBAL_ROLES = {}));
3261
3261
 
3262
- const AcademeAuthProvider = ({ realm, hubUrl, children, clientId, keycloakUrl, apiBaseUrl, }) => {
3263
- return (jsx(ReactKeycloakProvider, { authClient: new Keycloak({ clientId, realm, url: keycloakUrl }), children: jsx(SecurityProvider, { hubUrl: hubUrl, apiBaseUrl: apiBaseUrl, children: children }) }));
3262
+ // --------------------------------------------------------
3263
+ // 1. SINGLETON: Variável declarada FORA do componente
3264
+ // Isso garante que ela sobreviva a re-renders do React
3265
+ // --------------------------------------------------------
3266
+ let globalKeycloak;
3267
+ const AcademeAuthProvider = ({ realm, hubUrl, children, clientId, keycloakUrl, apiBaseUrl, initOptions, skipApiUserFetch, }) => {
3268
+ // 2. Inicialização: Só cria se ainda não existir (Singleton)
3269
+ if (!globalKeycloak) {
3270
+ console.log("🦁 [AcademeAuthProvider] Criando Instância Única do Keycloak");
3271
+ globalKeycloak = new Keycloak({ clientId, realm, url: keycloakUrl });
3272
+ }
3273
+ // Mantemos o initOptions memorizado, mas forçamos o logging para debug
3274
+ const keycloakInitOptions = useMemo(() => ({
3275
+ ...initOptions,
3276
+ enableLogging: true, // <-- Importante para vermos o fluxo no console
3277
+ }), [initOptions]);
3278
+ return (jsx(ReactKeycloakProvider, { authClient: globalKeycloak, initOptions: keycloakInitOptions, children: jsx(SecurityProvider, { hubUrl: hubUrl, apiBaseUrl: apiBaseUrl, skipApiUserFetch: skipApiUserFetch, children: children }) }));
3264
3279
  };
3265
3280
  const SecurityContext = createContext({
3266
3281
  isInitialized: false,
@@ -3274,12 +3289,12 @@ const SecurityContext = createContext({
3274
3289
  isAuthenticated: () => false,
3275
3290
  apiClient: null,
3276
3291
  services: null,
3292
+ accessToken: undefined,
3277
3293
  });
3278
- const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", children, }) => {
3294
+ const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", skipApiUserFetch = false, children, }) => {
3279
3295
  const [isInitialized, setIsInitialized] = useState(false);
3280
3296
  const [currentUser, setCurrentUser] = useState(null);
3281
3297
  const { initialized, keycloak } = useKeycloak();
3282
- // Create API client with the provided baseUrl
3283
3298
  const apiClient = useMemo(() => {
3284
3299
  return createAcademeApiClient(apiBaseUrl);
3285
3300
  }, [apiBaseUrl]);
@@ -3300,6 +3315,7 @@ const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", child
3300
3315
  setIsInitialized(initialized);
3301
3316
  }, [initialized]);
3302
3317
  useEffect(() => {
3318
+ // @ts-ignore
3303
3319
  window.accessToken = keycloak.token;
3304
3320
  }, [keycloak.token]);
3305
3321
  const getKeycloakUser = useCallback(() => {
@@ -3310,10 +3326,16 @@ const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", child
3310
3326
  lastName: idTokenParsed?.family_name || "",
3311
3327
  };
3312
3328
  }, [keycloak?.idTokenParsed]);
3313
- // Fetch user data from API when authenticated
3314
3329
  useEffect(() => {
3315
3330
  const fetchUserData = async () => {
3316
3331
  if (initialized && keycloak?.authenticated && keycloak?.token) {
3332
+ if (skipApiUserFetch) {
3333
+ const academeUser = {
3334
+ keycloakUser: getKeycloakUser(),
3335
+ };
3336
+ setCurrentUser({ ...academeUser, id: keycloak?.idTokenParsed?.sub });
3337
+ return;
3338
+ }
3317
3339
  try {
3318
3340
  const response = await services.user.getMe();
3319
3341
  if (response?.data?.data) {
@@ -3339,9 +3361,17 @@ const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", child
3339
3361
  keycloak?.token,
3340
3362
  getKeycloakUser,
3341
3363
  services,
3364
+ skipApiUserFetch,
3342
3365
  ]);
3343
3366
  const refreshUserData = useCallback(async () => {
3344
3367
  if (keycloak?.authenticated) {
3368
+ if (skipApiUserFetch) {
3369
+ const academeUser = {
3370
+ keycloakUser: getKeycloakUser(),
3371
+ };
3372
+ setCurrentUser(academeUser);
3373
+ return;
3374
+ }
3345
3375
  try {
3346
3376
  const response = await services.user.getMe();
3347
3377
  if (response?.data?.data) {
@@ -3356,7 +3386,7 @@ const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", child
3356
3386
  console.error("Error refreshing user data:", error);
3357
3387
  }
3358
3388
  }
3359
- }, [keycloak?.authenticated, getKeycloakUser, services]);
3389
+ }, [keycloak?.authenticated, getKeycloakUser, services, skipApiUserFetch]);
3360
3390
  const signOut = () => {
3361
3391
  setCurrentUser(null);
3362
3392
  keycloak?.logout();
@@ -3383,6 +3413,7 @@ const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", child
3383
3413
  goToLogin: keycloak.login,
3384
3414
  hasRealmRole: keycloak?.hasRealmRole,
3385
3415
  hasClientRole: keycloak.hasResourceRole,
3416
+ accessToken: keycloak?.token,
3386
3417
  apiClient,
3387
3418
  services,
3388
3419
  }, children: children }));
@@ -6555,6 +6586,13 @@ var DASHBOARD_ROLES;
6555
6586
  (function (DASHBOARD_ROLES) {
6556
6587
  })(DASHBOARD_ROLES || (DASHBOARD_ROLES = {}));
6557
6588
 
6589
+ var APPLICATIONS_ROLES;
6590
+ (function (APPLICATIONS_ROLES) {
6591
+ APPLICATIONS_ROLES["ACCESS_NINA"] = "Acesso nina";
6592
+ APPLICATIONS_ROLES["ACCESS_MIKE"] = "Acesso mike";
6593
+ APPLICATIONS_ROLES["VIEW_WIDGET"] = "Visualizar Widget";
6594
+ })(APPLICATIONS_ROLES || (APPLICATIONS_ROLES = {}));
6595
+
6558
6596
  var index = /*#__PURE__*/Object.freeze({
6559
6597
  __proto__: null
6560
6598
  });
@@ -6568,5 +6606,5 @@ var academeApi = /*#__PURE__*/Object.freeze({
6568
6606
  __proto__: null
6569
6607
  });
6570
6608
 
6571
- export { AcademeAuthProvider, BACKOFFICE_ROLES, Button, DASHBOARD_ROLES, GLOBAL_ROLES, ProtectedApp, ProtectedComponent, ProtectedRouter, Spinner, academeApi as apiTypes, cn, createAcademeApiClient, index as types, useAcademeAuth };
6609
+ export { APPLICATIONS_ROLES, AcademeAuthProvider, BACKOFFICE_ROLES, Button, DASHBOARD_ROLES, GLOBAL_ROLES, ProtectedApp, ProtectedComponent, ProtectedRouter, Spinner, academeApi as apiTypes, cn, createAcademeApiClient, index as types, useAcademeAuth };
6572
6610
  //# sourceMappingURL=index.esm.js.map