Created
August 11, 2018 16:15
-
-
Save weyert/0a4a7adb4bfca29410241cd906e350b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const DashboardRoute = (props) => { | |
const { component: Component, isAuthenticated, ...rest } = props; | |
return ( | |
<Route | |
{...rest} | |
render={matchProps => { | |
return isAuthenticated ? ( | |
<DashboardLayout> | |
<Component {...props} /> | |
</DashboardLayout> | |
) : ( | |
<Redirect | |
to={{ pathname: '/login', state: { from: matchProps.location.pathname } }} | |
/> | |
); | |
}} | |
/> | |
); | |
}; | |
const mapStateToProps = state => { | |
return { | |
isAuthenticated: getIsAuthenticated(state) | |
}; | |
}; | |
export default connect(mapStateToProps)(DashboardRoute); |
This file contains hidden or 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
onLogin = ({ username, password }) => { | |
this.setState({ isLoading: true }); | |
this.props.login(username, password).then(({ payload: sessionData }) => { | |
this.setState({ isLoading: false }); | |
const profileData = sessionData.role; | |
const defaultRedirectUrl = getRedirectUrlByUserRole(profileData); | |
// Check if the state of the location has the from defined, if so we should redirect to that instead | |
const { location } = this.props | |
const { state: locationState = {} } = location | |
const { from = {} } = locationState | |
// Reset the location state for the login page, before redirect | |
const { from: fromLocation, ...newState } = locationState | |
this.props.history.replace({ | |
...this.props.location.pathname, | |
state: {} | |
}) | |
if (from && from !== '' && from !== '/') { | |
const redirectUrl = typeof from === 'string' ? from : defaultRedirectUrl | |
this.props.history.push(redirectUrl, { | |
from: null, | |
}); | |
} else { | |
this.props.history.push(defaultRedirectUrl, { | |
from: null, | |
}); | |
} | |
}).catch((err) => { | |
this.setState({ isLoading: false }); | |
console.error(err); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment