Skip to content
Snippets Groups Projects
Select Git revision
  • afbf588b5e2c84bf0f5fc4da6bb409c5e56ffcfd
  • main default protected
  • userHandling
  • snuggle
4 results

PrivateRoute.jsx

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    PrivateRoute.jsx 905 B
    import React from 'react';
    import { Navigate, useLocation } from 'react-router-dom';
    import { useAuth } from '../contexts/Auth/AuthState';
    
    function PrivateRoute({ children }) {
      // #################################
      // HOOKS
      // #################################
      // ### MAKE USE OF location
      const location = useLocation();
    
      // #################################
      // FUNCTIONS
      // #################################
      // ### IMPORT CurrentUser FUNCTION FROM AuthContext
      const { currentUser } = useAuth();
    
      // #################################
      // OUTPUT
      // #################################
      // if user is logged in, load children
      // otherwise load login page, but remember originally requested page in location state.redirectTo
      return currentUser ? children : <Navigate to='/login' replace state={{ redirectTo: location }} />;
    }
    
    export default PrivateRoute;