# with-login

> React hoc component for rendering page only on user log success. It uses [redux-saga-data](https://github.com/betagouv/redux-saga-data) as implicit helper.

Latest version **2.4.0-fix1** (published 2019-06-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install with-login
pnpm add with-login
yarn add with-login
bun add with-login
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.0-fix1 |
| Published | 2019-06-06 |
| First published | 2019-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Unpacked size | 283.6 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | yes |
| GitHub stars | 0 |
| Maintainers | ledoux |
| Keywords | react, hoc, login, redux, request-saga-data |

## Links

- npm: https://www.npmjs.com/package/with-login
- Repository: https://github.com/betagouv/with-login
- Homepage: https://github.com/betagouv/with-login#readme
- Issues: https://github.com/betagouv/with-login/issues
- npm.io page: https://npm.io/package/with-login

## Dependencies (12)

- [uuid](https://npm.io/package/uuid.md) ^3.3.2
- [react](https://npm.io/package/react.md) ^16.5.2
- [redux](https://npm.io/package/redux.md) ^4.0.0
- [eslint](https://npm.io/package/eslint.md) ^5.8.0
- [reselect](https://npm.io/package/reselect.md) ^3.0.1
- [react-dom](https://npm.io/package/react-dom.md) ^16.5.2
- [classnames](https://npm.io/package/classnames.md) 2.2.5
- [prop-types](https://npm.io/package/prop-types.md) ^15.6.2
- [lodash.uniq](https://npm.io/package/lodash.uniq.md) ^4.5.0
- [react-redux](https://npm.io/package/react-redux.md) ^5.0.7
- [redux-saga-data](https://npm.io/package/redux-saga-data.md) ^1.8.0
- [react-router-dom](https://npm.io/package/react-router-dom.md) ^4.3.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.4.0-fix1 (latest) — 2019-06-06
- 3.5.1 — 2019-04-12
- 3.5.0 — 2019-04-12
- 3.4.0 — 2019-04-12
- 3.3.0 — 2019-04-12
- 3.2.0 — 2019-04-12
- 3.1.0 — 2019-04-12
- 3.0.0 — 2019-04-12
- 2.4.0 — 2019-03-14
- 2.3.1 — 2019-03-14
- 2.3.0 — 2019-03-13
- 2.2.0 — 2019-03-11
- 2.1.0 — 2019-03-11
- 2.0.0 — 2019-03-09
- 1.0.0 — 2019-02-28
- … 8 more at https://npm.io/package/with-login/versions

## README

# with-login

React hoc component for rendering page only on user log success. It uses [redux-saga-data](https://github.com/betagouv/redux-saga-data) as implicit helper.

[![CircleCI](https://circleci.com/gh/betagouv/with-login/tree/master.svg?style=svg)](https://circleci.com/gh/betagouv/with-login/tree/master)
[![npm version](https://img.shields.io/npm/v/with-login.svg?style=flat-square)](https://npmjs.org/package/with-login)

## Basic Usage
You need to add first the data reducer in your root reducer:

You need to install a redux-saga setup with the watchDataActions and the data reducer,
don't forget to specify the url of your api here:

```javascript
import {
  applyMiddleware,
  combineReducers,
  createStore
} from 'redux'
import createSagaMiddleware from 'redux-saga'
import { all } from 'redux-saga/effects'
import { createData, watchDataActions } from 'redux-saga-data'

const sagaMiddleware = createSagaMiddleware()
const storeEnhancer = applyMiddleware(sagaMiddleware)

function* rootSaga() {
  yield all([
    watchDataActions({
      rootUrl: "https://myfoo.com",
    }),
  ])
}

const rootReducer = combineReducers({
  data: createData({ users: [] }),
})

const store = createStore(rootReducer, storeEnhancer)

sagaMiddleware.run(rootSaga)
```

Then you can use withLogin in your component:

```javascript

import withLogin from 'with-login'

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

export default withLogin({
  currentUserApiPath: '/users/current',
  failRedirect: '/signin'
})(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

### withLogin

| name | type | example | isRequired | default | description |
| -- | -- | -- | -- | -- | -- |
| currentUserApiPath | `string` | [See test](https://github.com/betagouv/normalized-data-state/blob/887323e6146d5eec40203b4f4b692bfcb65a4cd9/src/tests/getNormalizedMergedState.spec.js#L92) | no | '/users/current' | apiPath that will be joined with your rootUrl to get the authenticated user from your auth server |
| failRedirect | `function` | [See test](https://github.com/betagouv/normalized-data-state/blob/887323e6146d5eec40203b4f4b692bfcb65a4cd9/src/tests/getNormalizedMergedState.spec.js#L92) | no | 'undefined' | function triggered after fail of your auth currentUserApiPath request saying. It should return a redirect path towards which react-router will history push. |
| successRedirect | `function` | [See test](https://github.com/betagouv/normalized-data-state/blob/887323e6146d5eec40203b4f4b692bfcb65a4cd9/src/tests/getNormalizedMergedState.spec.js#L92) | no | 'undefined' | function triggered after success of your auth currentUserApiPath request saying. It should return a redirect path towards which react-router will history push. |
| isRequired | `boolean` | [See test](https://github.com/betagouv/normalized-data-state/blob/887323e6146d5eec40203b4f4b692bfcb65a4cd9/src/tests/getNormalizedMergedState.spec.js#L92) | no | 'true' | Boolean saying if the React WrappedComponent will need to wait a success from the currentUserApiPath to be rendered. |

---
_Source: https://npm.io/package/with-login · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
