Last active
May 16, 2022 02:13
-
-
Save sscotth/528b046837cb104bd975f6a28cde3673 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <Routes> | |
// <Route path="/" element={<RequireAuth><Dashboard /></RequireAuth>}> | |
// <Route path="about" element={<AboutPage />} /> | |
// </Routes> | |
import { Auth } from 'aws-amplify' | |
import { Navigate } from 'react-router-dom' | |
const RequireAuth = ({ children }) => { | |
const [loading, setLoading] = React.useState(true) | |
const [user, setUser] = React.useState() | |
React.useEffect(() => { | |
Auth | |
.currentAuthenticatedUser() | |
.then(u => { | |
setUser(u) | |
setLoading(false) | |
}) | |
.catch((err) => { | |
setLoading(false) | |
console.error(err) | |
}) | |
}, []) | |
if (loading) { | |
return <h1>Loading</h1> | |
} | |
if (user) { | |
return children | |
} | |
return <Navigate to="/login" replace /> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment