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