@riligar/auth-react 1.6.2 → 1.7.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 +3 -104
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +27 -128
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
package/dist/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var zustand = require('zustand');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var shallow = require('zustand/react/shallow');
|
|
4
6
|
var reactRouterDom = require('react-router-dom');
|
|
5
7
|
var core = require('@mantine/core');
|
|
6
8
|
var form = require('@mantine/form');
|
|
@@ -287,49 +289,8 @@ function socialRedirect(provider, redirectTo = typeof window !== 'undefined' ? w
|
|
|
287
289
|
window.location = `${API_BASE}/auth/sign-in/${provider}?redirect=${encodeURIComponent(redirectTo)}`;
|
|
288
290
|
}
|
|
289
291
|
|
|
290
|
-
const createStoreImpl = (createState) => {
|
|
291
|
-
let state;
|
|
292
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
293
|
-
const setState = (partial, replace) => {
|
|
294
|
-
const nextState = typeof partial === "function" ? partial(state) : partial;
|
|
295
|
-
if (!Object.is(nextState, state)) {
|
|
296
|
-
const previousState = state;
|
|
297
|
-
state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
|
|
298
|
-
listeners.forEach((listener) => listener(state, previousState));
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
const getState = () => state;
|
|
302
|
-
const getInitialState = () => initialState;
|
|
303
|
-
const subscribe = (listener) => {
|
|
304
|
-
listeners.add(listener);
|
|
305
|
-
return () => listeners.delete(listener);
|
|
306
|
-
};
|
|
307
|
-
const api = { setState, getState, getInitialState, subscribe };
|
|
308
|
-
const initialState = state = createState(setState, getState, api);
|
|
309
|
-
return api;
|
|
310
|
-
};
|
|
311
|
-
const createStore = ((createState) => createState ? createStoreImpl(createState) : createStoreImpl);
|
|
312
|
-
|
|
313
|
-
const identity = (arg) => arg;
|
|
314
|
-
function useStore(api, selector = identity) {
|
|
315
|
-
const slice = React$1.useSyncExternalStore(
|
|
316
|
-
api.subscribe,
|
|
317
|
-
React$1.useCallback(() => selector(api.getState()), [api, selector]),
|
|
318
|
-
React$1.useCallback(() => selector(api.getInitialState()), [api, selector])
|
|
319
|
-
);
|
|
320
|
-
React$1.useDebugValue(slice);
|
|
321
|
-
return slice;
|
|
322
|
-
}
|
|
323
|
-
const createImpl = (createState) => {
|
|
324
|
-
const api = createStore(createState);
|
|
325
|
-
const useBoundStore = (selector) => useStore(api, selector);
|
|
326
|
-
Object.assign(useBoundStore, api);
|
|
327
|
-
return useBoundStore;
|
|
328
|
-
};
|
|
329
|
-
const create = ((createState) => createState ? createImpl(createState) : createImpl);
|
|
330
|
-
|
|
331
292
|
// Estado: { user, loading, error }
|
|
332
|
-
const useAuthStore = create((set, get) => ({
|
|
293
|
+
const useAuthStore = zustand.create((set, get) => ({
|
|
333
294
|
user: null,
|
|
334
295
|
loading: true,
|
|
335
296
|
error: null,
|
|
@@ -712,69 +673,7 @@ const useAuthStore = create((set, get) => ({
|
|
|
712
673
|
})
|
|
713
674
|
}));
|
|
714
675
|
|
|
715
|
-
const
|
|
716
|
-
const hasIterableEntries = (value) => (
|
|
717
|
-
// HACK: avoid checking entries type
|
|
718
|
-
"entries" in value
|
|
719
|
-
);
|
|
720
|
-
const compareEntries = (valueA, valueB) => {
|
|
721
|
-
const mapA = valueA instanceof Map ? valueA : new Map(valueA.entries());
|
|
722
|
-
const mapB = valueB instanceof Map ? valueB : new Map(valueB.entries());
|
|
723
|
-
if (mapA.size !== mapB.size) {
|
|
724
|
-
return false;
|
|
725
|
-
}
|
|
726
|
-
for (const [key, value] of mapA) {
|
|
727
|
-
if (!mapB.has(key) || !Object.is(value, mapB.get(key))) {
|
|
728
|
-
return false;
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
return true;
|
|
732
|
-
};
|
|
733
|
-
const compareIterables = (valueA, valueB) => {
|
|
734
|
-
const iteratorA = valueA[Symbol.iterator]();
|
|
735
|
-
const iteratorB = valueB[Symbol.iterator]();
|
|
736
|
-
let nextA = iteratorA.next();
|
|
737
|
-
let nextB = iteratorB.next();
|
|
738
|
-
while (!nextA.done && !nextB.done) {
|
|
739
|
-
if (!Object.is(nextA.value, nextB.value)) {
|
|
740
|
-
return false;
|
|
741
|
-
}
|
|
742
|
-
nextA = iteratorA.next();
|
|
743
|
-
nextB = iteratorB.next();
|
|
744
|
-
}
|
|
745
|
-
return !!nextA.done && !!nextB.done;
|
|
746
|
-
};
|
|
747
|
-
function shallow(valueA, valueB) {
|
|
748
|
-
if (Object.is(valueA, valueB)) {
|
|
749
|
-
return true;
|
|
750
|
-
}
|
|
751
|
-
if (typeof valueA !== "object" || valueA === null || typeof valueB !== "object" || valueB === null) {
|
|
752
|
-
return false;
|
|
753
|
-
}
|
|
754
|
-
if (Object.getPrototypeOf(valueA) !== Object.getPrototypeOf(valueB)) {
|
|
755
|
-
return false;
|
|
756
|
-
}
|
|
757
|
-
if (isIterable(valueA) && isIterable(valueB)) {
|
|
758
|
-
if (hasIterableEntries(valueA) && hasIterableEntries(valueB)) {
|
|
759
|
-
return compareEntries(valueA, valueB);
|
|
760
|
-
}
|
|
761
|
-
return compareIterables(valueA, valueB);
|
|
762
|
-
}
|
|
763
|
-
return compareEntries(
|
|
764
|
-
{ entries: () => Object.entries(valueA) },
|
|
765
|
-
{ entries: () => Object.entries(valueB) }
|
|
766
|
-
);
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
function useShallow(selector) {
|
|
770
|
-
const prev = React$1.useRef(void 0);
|
|
771
|
-
return (state) => {
|
|
772
|
-
const next = selector(state);
|
|
773
|
-
return shallow(prev.current, next) ? prev.current : prev.current = next;
|
|
774
|
-
};
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
const AuthContext = /*#__PURE__*/React$1.createContext(); // só para ter o Provider em JSX
|
|
676
|
+
const AuthContext = /*#__PURE__*/react.createContext(); // só para ter o Provider em JSX
|
|
778
677
|
|
|
779
678
|
function AuthProvider({
|
|
780
679
|
children,
|
|
@@ -798,19 +697,19 @@ function AuthProvider({
|
|
|
798
697
|
// Configura SDK com apiUrl e apiKey
|
|
799
698
|
// Configura SDK com apiUrl e apiKey
|
|
800
699
|
// Usamos useMemo para garantir que a configuração ocorra ANTES dos efeitos dos componentes filhos
|
|
801
|
-
|
|
700
|
+
react.useMemo(() => {
|
|
802
701
|
configure({
|
|
803
702
|
apiUrl,
|
|
804
703
|
apiKey
|
|
805
704
|
});
|
|
806
705
|
}, [apiUrl, apiKey]);
|
|
807
|
-
|
|
706
|
+
react.useEffect(() => {
|
|
808
707
|
init();
|
|
809
708
|
startRefresh();
|
|
810
709
|
}, [init, startRefresh]);
|
|
811
710
|
|
|
812
711
|
// Sincronização entre abas - escuta logout e mudanças no token
|
|
813
|
-
|
|
712
|
+
react.useEffect(() => {
|
|
814
713
|
if (typeof window === 'undefined') return;
|
|
815
714
|
const handleStorageChange = event => {
|
|
816
715
|
if (event.key === 'auth:logout') {
|
|
@@ -826,7 +725,7 @@ function AuthProvider({
|
|
|
826
725
|
}, [checkTokenValidity]);
|
|
827
726
|
|
|
828
727
|
// Verifica validade do token periodicamente
|
|
829
|
-
|
|
728
|
+
react.useEffect(() => {
|
|
830
729
|
if (typeof window === 'undefined') return;
|
|
831
730
|
const interval = setInterval(() => {
|
|
832
731
|
checkTokenValidity();
|
|
@@ -836,7 +735,7 @@ function AuthProvider({
|
|
|
836
735
|
}, [checkTokenValidity]);
|
|
837
736
|
|
|
838
737
|
// Contexto com onError callback
|
|
839
|
-
const contextValue =
|
|
738
|
+
const contextValue = react.useMemo(() => ({
|
|
840
739
|
onError
|
|
841
740
|
}), [onError]);
|
|
842
741
|
return /*#__PURE__*/React.createElement(AuthContext.Provider, {
|
|
@@ -845,7 +744,7 @@ function AuthProvider({
|
|
|
845
744
|
}
|
|
846
745
|
|
|
847
746
|
/* Hooks "facade" que a app vai usar */
|
|
848
|
-
const useAuth = () => useAuthStore(useShallow(s => ({
|
|
747
|
+
const useAuth = () => useAuthStore(shallow.useShallow(s => ({
|
|
849
748
|
user: s.user,
|
|
850
749
|
loading: s.loading,
|
|
851
750
|
error: s.error,
|
|
@@ -859,7 +758,7 @@ const useSignOut = () => useAuthStore(s => s.signOut);
|
|
|
859
758
|
const useCheckToken = () => useAuthStore(s => s.checkTokenValidity);
|
|
860
759
|
|
|
861
760
|
// Magic Link Hook
|
|
862
|
-
const useMagicLink = () => useAuthStore(useShallow(s => ({
|
|
761
|
+
const useMagicLink = () => useAuthStore(shallow.useShallow(s => ({
|
|
863
762
|
sendMagicLink: s.sendMagicLink,
|
|
864
763
|
verifyMagicLink: s.verifyMagicLink,
|
|
865
764
|
loading: s.loadingStates.magicLink,
|
|
@@ -868,7 +767,7 @@ const useMagicLink = () => useAuthStore(useShallow(s => ({
|
|
|
868
767
|
})));
|
|
869
768
|
|
|
870
769
|
// Password Reset Hook
|
|
871
|
-
const usePasswordReset = () => useAuthStore(useShallow(s => ({
|
|
770
|
+
const usePasswordReset = () => useAuthStore(shallow.useShallow(s => ({
|
|
872
771
|
forgotPassword: s.forgotPassword,
|
|
873
772
|
resetPassword: s.resetPassword,
|
|
874
773
|
loading: s.loadingStates.resetPassword,
|
|
@@ -876,7 +775,7 @@ const usePasswordReset = () => useAuthStore(useShallow(s => ({
|
|
|
876
775
|
})));
|
|
877
776
|
|
|
878
777
|
// Email Verification Hook
|
|
879
|
-
const useEmailVerification = () => useAuthStore(useShallow(s => ({
|
|
778
|
+
const useEmailVerification = () => useAuthStore(shallow.useShallow(s => ({
|
|
880
779
|
verifyEmail: s.verifyEmail,
|
|
881
780
|
resendVerification: s.resendVerification,
|
|
882
781
|
loading: s.loadingStates.verifyEmail,
|
|
@@ -885,7 +784,7 @@ const useEmailVerification = () => useAuthStore(useShallow(s => ({
|
|
|
885
784
|
})));
|
|
886
785
|
|
|
887
786
|
// Session Hook
|
|
888
|
-
const useSession = () => useAuthStore(useShallow(s => ({
|
|
787
|
+
const useSession = () => useAuthStore(shallow.useShallow(s => ({
|
|
889
788
|
getSession: s.getSession,
|
|
890
789
|
user: s.user,
|
|
891
790
|
setUser: s.setUser
|
|
@@ -1333,15 +1232,15 @@ function MagicLinkVerify({
|
|
|
1333
1232
|
labels = {},
|
|
1334
1233
|
...cardProps
|
|
1335
1234
|
}) {
|
|
1336
|
-
const [status, setStatus] =
|
|
1337
|
-
const [errorMessage, setErrorMessage] =
|
|
1235
|
+
const [status, setStatus] = react.useState('verifying'); // verifying, success, error
|
|
1236
|
+
const [errorMessage, setErrorMessage] = react.useState('');
|
|
1338
1237
|
const verifyMagicLink = useAuthStore(s => s.verifyMagicLink);
|
|
1339
1238
|
|
|
1340
1239
|
// Hook para buscar logo da aplicação
|
|
1341
1240
|
const applicationLogo = useApplicationLogo();
|
|
1342
1241
|
const finalLogo = logo || applicationLogo || img;
|
|
1343
|
-
const verifyingTokenRef =
|
|
1344
|
-
|
|
1242
|
+
const verifyingTokenRef = react.useRef(null);
|
|
1243
|
+
react.useEffect(() => {
|
|
1345
1244
|
const verify = async () => {
|
|
1346
1245
|
// Pega token da prop ou da URL
|
|
1347
1246
|
let token = propToken;
|
|
@@ -1508,8 +1407,8 @@ function ResetPasswordForm({
|
|
|
1508
1407
|
labels = {},
|
|
1509
1408
|
...cardProps
|
|
1510
1409
|
}) {
|
|
1511
|
-
const [token, setToken] =
|
|
1512
|
-
const [success, setSuccess] =
|
|
1410
|
+
const [token, setToken] = react.useState(propToken || '');
|
|
1411
|
+
const [success, setSuccess] = react.useState(false);
|
|
1513
1412
|
const resetPassword = useAuthStore(s => s.resetPassword);
|
|
1514
1413
|
const loading = useAuthStore(s => s.loadingStates.resetPassword);
|
|
1515
1414
|
|
|
@@ -1518,7 +1417,7 @@ function ResetPasswordForm({
|
|
|
1518
1417
|
const finalLogo = logo || applicationLogo || img;
|
|
1519
1418
|
|
|
1520
1419
|
// Extrai token da URL se não foi passado como prop
|
|
1521
|
-
|
|
1420
|
+
react.useEffect(() => {
|
|
1522
1421
|
if (!propToken && typeof window !== 'undefined') {
|
|
1523
1422
|
const urlParams = new URLSearchParams(window.location.search);
|
|
1524
1423
|
const urlToken = urlParams.get('token');
|
|
@@ -1637,20 +1536,20 @@ function VerifyEmailCard({
|
|
|
1637
1536
|
labels = {},
|
|
1638
1537
|
...cardProps
|
|
1639
1538
|
}) {
|
|
1640
|
-
const [status, setStatus] =
|
|
1641
|
-
const [errorMessage, setErrorMessage] =
|
|
1642
|
-
const [tokenEmail, setTokenEmail] =
|
|
1539
|
+
const [status, setStatus] = react.useState('verifying'); // verifying, success, error
|
|
1540
|
+
const [errorMessage, setErrorMessage] = react.useState('');
|
|
1541
|
+
const [tokenEmail, setTokenEmail] = react.useState(null); // Novo estado para email do token
|
|
1643
1542
|
|
|
1644
1543
|
const verifyEmail = useAuthStore(s => s.verifyEmail);
|
|
1645
1544
|
const resendVerification = useAuthStore(s => s.resendVerification);
|
|
1646
1545
|
const loadingResend = useAuthStore(s => s.loadingStates.resendVerification);
|
|
1647
1546
|
const user = useAuthStore(s => s.user);
|
|
1648
|
-
const verifyingTokenRef =
|
|
1547
|
+
const verifyingTokenRef = react.useRef(null);
|
|
1649
1548
|
|
|
1650
1549
|
// Hook para buscar logo da aplicação
|
|
1651
1550
|
const applicationLogo = useApplicationLogo();
|
|
1652
1551
|
const finalLogo = logo || applicationLogo || img;
|
|
1653
|
-
|
|
1552
|
+
react.useEffect(() => {
|
|
1654
1553
|
const verify = async () => {
|
|
1655
1554
|
// Pega token da prop ou da URL
|
|
1656
1555
|
let token = propToken;
|