Created
December 16, 2020 16:11
-
-
Save ashug0001/dee1e6d19ed7e93da10e46d75193a7d2 to your computer and use it in GitHub Desktop.
Protected Route for React for Redux
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
import React from "react"; | |
import { Route, Redirect } from "react-router-dom"; | |
import { connect } from "react-redux"; | |
import PropType from "prop-types"; | |
const ProtectedRoutes = ({ isLoggedIn, children, ...rest }) => { | |
return isLoggedIn ? ( | |
<Route {...rest} render={(props) => children} /> | |
) : ( | |
<Redirect to="/login" /> | |
); | |
}; | |
ProtectedRoutes.propType = { | |
isLoggedIn: PropType.bool.isRequired, | |
}; | |
const mapState = (state) => { | |
const isLoggedIn = state.auth.isLoggedIn; | |
return { | |
isLoggedIn, | |
}; | |
}; | |
export default connect(null, null)(ProtectedRoutes); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment