Download client/src/hooks/useRequireAuth.js from Jessedbrown/onboarding: direct link, hf CLI and curl.
- Browser
- Download file 693 Bytes
-
https://huggingface.co/Jessedbrown/onboarding/resolve/main/client/src/hooks/useRequireAuth.js
- Command line
-
hf download hf://Jessedbrown/onboarding/client/src/hooks/useRequireAuth.js
-
curl -L -o useRequireAuth.js https://huggingface.co/Jessedbrown/onboarding/resolve/main/client/src/hooks/useRequireAuth.js
693 Bytes
| import { useEffect } from 'react'; | |
| import { useHistory, useLocation } from 'react-router-dom'; | |
| import { useAuth } from '../../../contexts/AuthContext'; | |
| import { ROUTES } from '../../../utils/constants'; | |
| export const useRequireAuth = (redirectUrl = ROUTES.LOGIN) => { | |
| const { user, isAuthenticated, isLoading } = useAuth(); | |
| const history = useHistory(); | |
| const location = useLocation(); | |
| useEffect(() => { | |
| if (!isLoading && !isAuthenticated) { | |
| history.replace({ | |
| pathname: redirectUrl, | |
| state: { from: location.pathname } | |
| }); | |
| } | |
| }, [user, isAuthenticated, isLoading, history, location, redirectUrl]); | |
| return { user, isAuthenticated, isLoading }; | |
| }; |