@riligar/auth-react 1.21.0 → 1.23.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
@@ -752,32 +752,39 @@ const useAuthStore = zustand.create((set, get) => ({
752
752
  if (isAuthenticated()) {
753
753
  const token = window.localStorage.getItem('auth:token');
754
754
  if (token) {
755
- // Decodifica o token para verificar tempo de expiração
756
- const base64Url = token.split('.')[1];
757
- const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
758
- const jsonPayload = window.atob(base64);
759
- const payload = JSON.parse(jsonPayload);
755
+ // Usa o decoder oficial do SDK que é mais seguro
756
+ const payload = decodeJWT(token);
757
+
758
+ // Se não for um JWT ou não tiver expiração, não fazemos refresh em background
759
+ // O backend cuidará da expiração da sessão opaca via 401 nas requisições normais
760
+ if (!payload || !payload.exp) return;
760
761
  const now = Date.now() / 1000;
761
762
  const timeUntilExpiry = payload.exp - now;
762
763
 
763
- // Se o token expira em menos de 5 minutos, faz refresh
764
+ // Se o token expira em menos de 5 minutos, tenta o refresh
764
765
  if (timeUntilExpiry < 300) {
765
- // 5 minutos
766
- await refreshToken();
767
- const user = getCurrentUser();
768
- set({
769
- user
770
- });
766
+ try {
767
+ await refreshToken();
768
+ const user = getCurrentUser();
769
+ set({
770
+ user
771
+ });
772
+ } catch (refreshErr) {
773
+ console.warn('[AuthStore] Falha ao renovar token:', refreshErr);
774
+ // Só desloga se for um erro de autenticação explícito (401)
775
+ if (refreshErr.res?.status === 401) {
776
+ set({
777
+ user: null
778
+ });
779
+ window.localStorage.removeItem('auth:token');
780
+ }
781
+ }
771
782
  }
772
783
  }
773
784
  }
774
785
  } catch (error) {
775
- console.error('Erro no refresh automático:', error);
776
- // Em caso de erro, faz logout
777
- set({
778
- user: null
779
- });
780
- window.localStorage.removeItem('auth:token');
786
+ // Erros de processamento interno não devem deslogar o usuário
787
+ console.error('[AuthStore] Erro no ciclo de refresh automático:', error);
781
788
  }
782
789
  }, 4 * 60 * 1000); // Verifica a cada 4 minutos
783
790
 
@@ -969,7 +976,10 @@ const useAuth = () => useAuthStore(shallow.useShallow(s => ({
969
976
  user: s.user,
970
977
  loading: s.loading,
971
978
  error: s.error,
972
- isAuthenticated: s.user !== null
979
+ isAuthenticated: s.user !== null,
980
+ signIn: s.signIn,
981
+ signUp: s.signUp,
982
+ signOut: s.signOut
973
983
  })));
974
984
 
975
985
  // Auth Actions