with-react-redux-login
v1.22.7
Published
React hoc component for rendering page only on user sign success. It uses fetch-normalize-data as implicit helper.
Downloads
24
Readme
with-react-redux-login
React Redux hoc component for rendering page only on user log success.
Basic Usage
Using redux-saga-data or redux-thunk-data
See first the store install process in redux-thunk-data.
Then you can declare a login component like this:
import { withRouter } from 'react-router-dom'
import { compose } from 'redux'
import withLogin from 'with-react-redux-login'
const withLoginRedirectToSigninWhenNotAuthenticated = compose(
withRouter,
withLogin({
currentUserApiPath: '/users/current',
handleFail: (state, action, ownProps) => {
const { history } = ownProps
history.push('/signin')
},
})
)
const FooPage = () => {
// withLogin passes a currentUser props
const { currentUser } = this.props
const { email } = currentUser || {}
return (
<div>
I am connected with {email} !
</div>
)
}
export default compose(withLoginRedirectToSigninWhenNotAuthenticated)(FooPage)
Depending on what returns GET 'https://myfoo.com/users/current':
- if it is a 200 with { email: '[email protected]' }, FooPage will be rendered,
- if it is a 400, app will redirect to '/signin' page.
Usage with config
See first the store install process in fetch-normalize-data.