academe-kit 0.5.5 → 0.5.7

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.cjs CHANGED
@@ -5855,15 +5855,91 @@ const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", skipA
5855
5855
  // Ref para armazenar o resolver da Promise de token
5856
5856
  const tokenReadyResolverRef = React2.useRef(null);
5857
5857
  const tokenReadyPromiseRef = React2.useRef(null);
5858
+ // Estados para controle de token injetado pelo mobile
5859
+ const [mobileToken, setMobileToken] = React2.useState(undefined);
5860
+ const [isMobileWebView, setIsMobileWebView] = React2.useState(false);
5858
5861
  // Extrair valores primitivos do auth para usar como dependências estáveis
5859
5862
  const isAuthenticated = auth.isAuthenticated;
5860
5863
  const isLoading = auth.isLoading;
5861
5864
  const activeNavigator = auth.activeNavigator;
5862
- const accessToken = auth.user?.access_token;
5865
+ const oidcAccessToken = auth.user?.access_token;
5863
5866
  const userProfile = auth.user?.profile;
5864
5867
  const userProfileSub = auth.user?.profile?.sub;
5868
+ // Token a ser usado: prioriza mobile, depois OIDC
5869
+ const accessToken = isMobileWebView ? mobileToken : oidcAccessToken;
5870
+ // --- 0. Detectar Mobile WebView e escutar eventos de token ---
5871
+ React2.useEffect(() => {
5872
+ if (typeof window === "undefined")
5873
+ return;
5874
+ let pollingInterval = null;
5875
+ let pollingAttempts = 0;
5876
+ const MAX_POLLING_ATTEMPTS = 50; // 5 segundos máximo (50 * 100ms)
5877
+ // Função para verificar e configurar o token mobile
5878
+ const checkAndSetMobileToken = () => {
5879
+ if (window.keycloakConfig?.fromMobile && window.keycloakConfig?.token) {
5880
+ console.log('[SecurityProvider] Token mobile detectado via polling');
5881
+ setIsMobileWebView(true);
5882
+ setMobileToken(window.keycloakConfig.token);
5883
+ return true;
5884
+ }
5885
+ return false;
5886
+ };
5887
+ // 1. Verificação imediata
5888
+ if (checkAndSetMobileToken()) {
5889
+ console.log('[SecurityProvider] Token encontrado na verificação inicial');
5890
+ }
5891
+ else {
5892
+ // 2. Polling: verificar a cada 100ms até encontrar o token ou atingir o limite
5893
+ console.log('[SecurityProvider] Iniciando polling para detectar token mobile...');
5894
+ pollingInterval = setInterval(() => {
5895
+ pollingAttempts++;
5896
+ if (checkAndSetMobileToken()) {
5897
+ console.log(`[SecurityProvider] Token encontrado após ${pollingAttempts} tentativas`);
5898
+ if (pollingInterval) {
5899
+ clearInterval(pollingInterval);
5900
+ pollingInterval = null;
5901
+ }
5902
+ }
5903
+ else if (pollingAttempts >= MAX_POLLING_ATTEMPTS) {
5904
+ console.log('[SecurityProvider] Polling encerrado - token não encontrado (não é mobile WebView)');
5905
+ if (pollingInterval) {
5906
+ clearInterval(pollingInterval);
5907
+ pollingInterval = null;
5908
+ }
5909
+ }
5910
+ }, 100);
5911
+ }
5912
+ // 3. Escutar evento de injeção (fallback)
5913
+ const handleInjection = (event) => {
5914
+ console.log('[SecurityProvider] Token recebido via evento keycloakConfigInjected');
5915
+ setIsMobileWebView(true);
5916
+ setMobileToken(event.detail.token);
5917
+ // Parar polling se ainda estiver rodando
5918
+ if (pollingInterval) {
5919
+ clearInterval(pollingInterval);
5920
+ pollingInterval = null;
5921
+ }
5922
+ };
5923
+ // 4. Escutar evento de refresh do token
5924
+ const handleTokenRefresh = (event) => {
5925
+ console.log('[SecurityProvider] Token atualizado via evento keycloakTokenRefreshed');
5926
+ setMobileToken(event.detail.token);
5927
+ };
5928
+ window.addEventListener("keycloakConfigInjected", handleInjection);
5929
+ window.addEventListener("keycloakTokenRefreshed", handleTokenRefresh);
5930
+ return () => {
5931
+ if (pollingInterval) {
5932
+ clearInterval(pollingInterval);
5933
+ }
5934
+ window.removeEventListener("keycloakConfigInjected", handleInjection);
5935
+ window.removeEventListener("keycloakTokenRefreshed", handleTokenRefresh);
5936
+ };
5937
+ }, []);
5865
5938
  // --- 1. Silent Check Inicial (Check SSO) ---
5866
5939
  React2.useEffect(() => {
5940
+ // Não tentar silent login no mobile WebView
5941
+ if (isMobileWebView)
5942
+ return;
5867
5943
  if (!isAuthenticated &&
5868
5944
  !isLoading &&
5869
5945
  !activeNavigator &&
@@ -5874,7 +5950,7 @@ const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", skipA
5874
5950
  });
5875
5951
  }
5876
5952
  // eslint-disable-next-line react-hooks/exhaustive-deps
5877
- }, [isAuthenticated, isLoading, activeNavigator]);
5953
+ }, [isAuthenticated, isLoading, activeNavigator, isMobileWebView]);
5878
5954
  // --- 2. Configuração de API e Services ---
5879
5955
  // Ref para armazenar o token atual (acessível no middleware)
5880
5956
  const currentTokenRef = React2.useRef(undefined);
@@ -6051,7 +6127,7 @@ const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", skipA
6051
6127
  user: currentUser,
6052
6128
  refreshUserData,
6053
6129
  signOut,
6054
- isAuthenticated: () => isAuthenticated,
6130
+ isAuthenticated: () => isMobileWebView ? !!mobileToken : isAuthenticated,
6055
6131
  hasSchool,
6056
6132
  goToLogin,
6057
6133
  hasRealmRole,
@@ -6073,6 +6149,8 @@ const SecurityProvider = ({ apiBaseUrl = "https://stg-api.academe.com.br", skipA
6073
6149
  accessToken,
6074
6150
  apiClient,
6075
6151
  services,
6152
+ isMobileWebView,
6153
+ mobileToken,
6076
6154
  ]);
6077
6155
  return (jsxRuntime.jsx(SecurityContext.Provider, { value: contextValue, children: children }));
6078
6156
  };