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