Skip to content

Instantly share code, notes, and snippets.

@sscotth
Last active May 16, 2022 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sscotth/528b046837cb104bd975f6a28cde3673 to your computer and use it in GitHub Desktop.
Save sscotth/528b046837cb104bd975f6a28cde3673 to your computer and use it in GitHub Desktop.
// <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