@riligar/auth-react 1.16.0 → 1.17.0

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.js CHANGED
@@ -25,14 +25,17 @@ const getEnvVar = key => {
25
25
  };
26
26
  let API_BASE = (typeof window !== 'undefined' && window?.location ? getEnvVar('VITE_API_BASE') : null) || 'http://localhost:3000';
27
27
  let API_KEY = null;
28
+ let INTERNAL_MODE = false; // Modo interno: não exige API Key (para aplicações same-domain)
28
29
 
29
- // Permite configurar a API base e key externamente (chamado pelo AuthProvider)
30
+ // Permite configurar a API base, key e modo interno externamente (chamado pelo AuthProvider)
30
31
  function configure({
31
32
  apiUrl,
32
- apiKey
33
+ apiKey,
34
+ internal = false
33
35
  }) {
34
36
  if (apiUrl) API_BASE = apiUrl.replace(/\/$/, ''); // Remove trailing slash
35
37
  if (apiKey) API_KEY = apiKey;
38
+ INTERNAL_MODE = internal;
36
39
  }
37
40
 
38
41
  // helper fetch pré-configurado
@@ -54,8 +57,8 @@ async function api(route, opts = {}) {
54
57
  headers.Authorization = `Bearer ${token}`;
55
58
  }
56
59
 
57
- // Adiciona API Key se configurada
58
- if (API_KEY) {
60
+ // Adiciona API Key se configurada e não estiver em modo interno
61
+ if (API_KEY && !INTERNAL_MODE) {
59
62
  headers['X-API-Key'] = API_KEY;
60
63
  }
61
64
  const res = await fetch(url, {
@@ -887,11 +890,14 @@ function AuthProvider({
887
890
  apiUrl,
888
891
  // URL base da API (OBRIGATÓRIA)
889
892
  apiKey,
890
- // API Key para header X-API-Key (OBRIGATÓRIA)
893
+ // API Key para header X-API-Key (obrigatória exceto em modo internal)
894
+ internal = false,
895
+ // Modo interno: não exige API Key (para aplicações same-domain como dashboard)
891
896
  onError // Callback de erro global
892
897
  }) {
893
898
  // Validação de props obrigatórias
894
- if (!apiKey) {
899
+ // apiKey só é obrigatória se não estiver em modo internal
900
+ if (!internal && !apiKey) {
895
901
  throw new Error('[@riligar/auth-react] apiKey é obrigatória no AuthProvider. ' + 'Obtenha sua API Key no dashboard em https://dashboard.myauth.click');
896
902
  }
897
903
  if (!apiUrl) {
@@ -901,15 +907,15 @@ function AuthProvider({
901
907
  const startRefresh = useAuthStore(s => s.startRefresh);
902
908
  const checkTokenValidity = useAuthStore(s => s.checkTokenValidity);
903
909
 
904
- // Configura SDK com apiUrl e apiKey
905
- // Configura SDK com apiUrl e apiKey
910
+ // Configura SDK com apiUrl, apiKey e modo interno
906
911
  // Usamos useMemo para garantir que a configuração ocorra ANTES dos efeitos dos componentes filhos
907
912
  react.useMemo(() => {
908
913
  configure({
909
914
  apiUrl,
910
- apiKey
915
+ apiKey,
916
+ internal
911
917
  });
912
- }, [apiUrl, apiKey]);
918
+ }, [apiUrl, apiKey, internal]);
913
919
  react.useEffect(() => {
914
920
  init();
915
921
  startRefresh();