AuthWrapper.tsx 898 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import React, { useEffect, useState } from 'react';
  2. import LoadingScreen from '../../../components/LoadingScreen';
  3. import { useAuthStore } from '../../../state/authStore';
  4. import Login from './Login';
  5. import Onboarding from './Onboarding';
  6. interface IProps {
  7. children: React.ReactNode;
  8. }
  9. const AuthWrapper: React.FC<IProps> = ({ children }) => {
  10. const [initialLoad, setInitialLoad] = useState(true);
  11. const { configured, user, me, fetchConfigured } = useAuthStore();
  12. useEffect(() => {
  13. const fetchUser = async () => {
  14. await me();
  15. await fetchConfigured();
  16. setInitialLoad(false);
  17. };
  18. if (!user) fetchUser();
  19. }, [fetchConfigured, me, user]);
  20. if (initialLoad && !user) {
  21. return <LoadingScreen />;
  22. }
  23. if (user) {
  24. return <>{children}</>;
  25. }
  26. if (!configured) {
  27. return <Onboarding />;
  28. }
  29. return <Login />;
  30. };
  31. export default AuthWrapper;