with-react-redux-login

1.22.7 • Public • Published

with-react-redux-login

React Redux hoc component for rendering page only on user log success.

CircleCI npm version

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: 'michel.momarx@youpi.fr' }, 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.

Dependents (0)

Package Sidebar

Install

npm i with-react-redux-login

Weekly Downloads

6

Version

1.22.7

License

MPL-2.0

Unpacked Size

37.1 kB

Total Files

12

Last publish

Collaborators

  • alwin
  • ledoux
  • dbaudry
  • atusi
  • aliochka
  • annemarie35
  • fmassin
  • nerocro