1.13.9 • Published 4 years ago

with-react-login v1.13.9

Weekly downloads
4
License
MPL-2.0
Repository
github
Last release
4 years ago

with-react-login

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

CircleCI npm version

Basic Usage

Using redux-saga-data

See first the store install process in redux-thunk-data.

Then you can declare a login component like this:

import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
import { compose } from 'redux'
import withLogin from 'with-react-login'

const withConnectedLogin = compose(
  withRouter,
  withLogin({
    currentUserApiPath: '/users/current',
    handleFail: (state, action, { history }) => history.push('/signin'),
    withDispatcher: connect(),
  })
)

const FooPage = () => {
  // withLogin passes a currentUser props
  const { currentUser } = this.props
  const { email } = currentUser || {}
  return (
    <div>
      I am connected with {email} !
    </div>
  )
}

export default withConnectedLogin(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.

Using redux-thunk-data

Like above, see the install process in redux-thunk-data.

Then you need just to slightly change setup:

import { connect } from 'react-redux'
import { requestData } from 'redux-thunk-data'
import { compose } from 'redux'
import withLogin from 'with-react-login'

const withConnectedLogin = compose(
  withRouter,
  withLogin({
    currentUserApiPath: '/users/current',
    handleFail: (state, action, { history }) => history.push('/signin'),
    requestData,
    withDispatcher: connect()
  })
)
...

Using react-hook-data

See redux-hook-data, but this is the same principle.

import { DataContext } from 'react-hook-data'
import withLogin from 'with-react-login'

const withConnectedLogin = withLogin({
  currentUserApiPath: '/users/current',
  handleFail: (state, action, { history }) => history.push('/signin'),
  withDispatcher: DataContext
})

Usage with config

withLogin

nametypeexampleisRequireddefaultdescription
currentUserApiPathstringno'/users/current'apiPath that will be joined with your rootUrl to get the authenticated user from your auth server
failRedirectfunctionSee testno'undefined'function triggered after fail of your auth currentUserApiPath request saying. It should return a redirect path towards which react-router will history push.
currentUserobjectno'null'object saying if withLogin needs to be rendered already with a currentUser. Useful when we want to do redux-persist login
requestDatafunctionSee testyesrequestData from fetch-normalize-dataaction creator which will trigger the action to request '/users/current'
successRedirectfunctionSee testno'undefined'function triggered after success of your auth currentUserApiPath request saying. It should return a redirect path towards which react-router will history push.
isRequiredbooleanSee testno'true'Boolean saying if the React WrappedComponent will need to wait a success from the currentUserApiPath to be rendered.
1.13.6

4 years ago

1.13.4

4 years ago

1.13.3

4 years ago

1.13.9

4 years ago

1.13.8

4 years ago

1.13.7

4 years ago

1.13.2

4 years ago

1.13.1

4 years ago

1.13.0

4 years ago

1.12.0-rc5

4 years ago

1.12.0-rc4

4 years ago

1.12.0-rc2

4 years ago

1.12.0-rc1

4 years ago

1.11.4

4 years ago

1.11.3

4 years ago

1.11.1

4 years ago

1.11.0

4 years ago

1.10.1

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.0

5 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago