@riligar/auth-react 1.3.0 → 1.4.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 CHANGED
@@ -672,6 +672,68 @@ const useAuthStore = create((set, get) => ({
672
672
  })
673
673
  }));
674
674
 
675
+ const isIterable = (obj) => Symbol.iterator in obj;
676
+ const hasIterableEntries = (value) => (
677
+ // HACK: avoid checking entries type
678
+ "entries" in value
679
+ );
680
+ const compareEntries = (valueA, valueB) => {
681
+ const mapA = valueA instanceof Map ? valueA : new Map(valueA.entries());
682
+ const mapB = valueB instanceof Map ? valueB : new Map(valueB.entries());
683
+ if (mapA.size !== mapB.size) {
684
+ return false;
685
+ }
686
+ for (const [key, value] of mapA) {
687
+ if (!mapB.has(key) || !Object.is(value, mapB.get(key))) {
688
+ return false;
689
+ }
690
+ }
691
+ return true;
692
+ };
693
+ const compareIterables = (valueA, valueB) => {
694
+ const iteratorA = valueA[Symbol.iterator]();
695
+ const iteratorB = valueB[Symbol.iterator]();
696
+ let nextA = iteratorA.next();
697
+ let nextB = iteratorB.next();
698
+ while (!nextA.done && !nextB.done) {
699
+ if (!Object.is(nextA.value, nextB.value)) {
700
+ return false;
701
+ }
702
+ nextA = iteratorA.next();
703
+ nextB = iteratorB.next();
704
+ }
705
+ return !!nextA.done && !!nextB.done;
706
+ };
707
+ function shallow(valueA, valueB) {
708
+ if (Object.is(valueA, valueB)) {
709
+ return true;
710
+ }
711
+ if (typeof valueA !== "object" || valueA === null || typeof valueB !== "object" || valueB === null) {
712
+ return false;
713
+ }
714
+ if (Object.getPrototypeOf(valueA) !== Object.getPrototypeOf(valueB)) {
715
+ return false;
716
+ }
717
+ if (isIterable(valueA) && isIterable(valueB)) {
718
+ if (hasIterableEntries(valueA) && hasIterableEntries(valueB)) {
719
+ return compareEntries(valueA, valueB);
720
+ }
721
+ return compareIterables(valueA, valueB);
722
+ }
723
+ return compareEntries(
724
+ { entries: () => Object.entries(valueA) },
725
+ { entries: () => Object.entries(valueB) }
726
+ );
727
+ }
728
+
729
+ function useShallow(selector) {
730
+ const prev = React$1.useRef(void 0);
731
+ return (state) => {
732
+ const next = selector(state);
733
+ return shallow(prev.current, next) ? prev.current : prev.current = next;
734
+ };
735
+ }
736
+
675
737
  const AuthContext = /*#__PURE__*/createContext(); // só para ter o Provider em JSX
676
738
 
677
739
  function AuthProvider({
@@ -741,12 +803,12 @@ function AuthProvider({
741
803
  }
742
804
 
743
805
  /* Hooks "facade" que a app vai usar */
744
- const useAuth = () => useAuthStore(s => ({
806
+ const useAuth = () => useAuthStore(useShallow(s => ({
745
807
  user: s.user,
746
808
  loading: s.loading,
747
809
  error: s.error,
748
810
  isAuthenticated: s.user !== null
749
- }));
811
+ })));
750
812
 
751
813
  // Auth Actions
752
814
  const useSignIn = () => useAuthStore(s => s.signIn);
@@ -755,62 +817,37 @@ const useSignOut = () => useAuthStore(s => s.signOut);
755
817
  const useCheckToken = () => useAuthStore(s => s.checkTokenValidity);
756
818
 
757
819
  // Magic Link Hook
758
- const useMagicLink = () => {
759
- const sendMagicLink = useAuthStore(s => s.sendMagicLink);
760
- const verifyMagicLink = useAuthStore(s => s.verifyMagicLink);
761
- const loading = useAuthStore(s => s.loadingStates.magicLink);
762
- const verifying = useAuthStore(s => s.loadingStates.verifyMagicLink);
763
- const error = useAuthStore(s => s.error);
764
- return {
765
- sendMagicLink,
766
- verifyMagicLink,
767
- loading,
768
- verifying,
769
- error
770
- };
771
- };
820
+ const useMagicLink = () => useAuthStore(useShallow(s => ({
821
+ sendMagicLink: s.sendMagicLink,
822
+ verifyMagicLink: s.verifyMagicLink,
823
+ loading: s.loadingStates.magicLink,
824
+ verifying: s.loadingStates.verifyMagicLink,
825
+ error: s.error
826
+ })));
772
827
 
773
828
  // Password Reset Hook
774
- const usePasswordReset = () => {
775
- const forgotPassword = useAuthStore(s => s.forgotPassword);
776
- const resetPassword = useAuthStore(s => s.resetPassword);
777
- const loading = useAuthStore(s => s.loadingStates.resetPassword);
778
- const error = useAuthStore(s => s.error);
779
- return {
780
- forgotPassword,
781
- resetPassword,
782
- loading,
783
- error
784
- };
785
- };
829
+ const usePasswordReset = () => useAuthStore(useShallow(s => ({
830
+ forgotPassword: s.forgotPassword,
831
+ resetPassword: s.resetPassword,
832
+ loading: s.loadingStates.resetPassword,
833
+ error: s.error
834
+ })));
786
835
 
787
836
  // Email Verification Hook
788
- const useEmailVerification = () => {
789
- const verifyEmail = useAuthStore(s => s.verifyEmail);
790
- const resendVerification = useAuthStore(s => s.resendVerification);
791
- const loading = useAuthStore(s => s.loadingStates.verifyEmail);
792
- const resending = useAuthStore(s => s.loadingStates.resendVerification);
793
- const error = useAuthStore(s => s.error);
794
- return {
795
- verifyEmail,
796
- resendVerification,
797
- loading,
798
- resending,
799
- error
800
- };
801
- };
837
+ const useEmailVerification = () => useAuthStore(useShallow(s => ({
838
+ verifyEmail: s.verifyEmail,
839
+ resendVerification: s.resendVerification,
840
+ loading: s.loadingStates.verifyEmail,
841
+ resending: s.loadingStates.resendVerification,
842
+ error: s.error
843
+ })));
802
844
 
803
845
  // Session Hook
804
- const useSession = () => {
805
- const getSession = useAuthStore(s => s.getSession);
806
- const user = useAuthStore(s => s.user);
807
- const setUser = useAuthStore(s => s.setUser);
808
- return {
809
- getSession,
810
- user,
811
- setUser
812
- };
813
- };
846
+ const useSession = () => useAuthStore(useShallow(s => ({
847
+ getSession: s.getSession,
848
+ user: s.user,
849
+ setUser: s.setUser
850
+ })));
814
851
 
815
852
  // Loading States Hook
816
853
  const useAuthLoading = () => useAuthStore(s => s.loadingStates);