# @reshuffle/react-auth

> React provider for working with Reshuffle authentication

Latest version **0.1.7** (published 2019-11-24) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @reshuffle/react-auth
pnpm add @reshuffle/react-auth
yarn add @reshuffle/react-auth
bun add @reshuffle/react-auth
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.7 |
| Published | 2019-11-24 |
| First published | 2019-11-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 34.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | binaris, taillogs |
| Keywords | binaris, reshuffle, authentication, react |

## Links

- npm: https://www.npmjs.com/package/@reshuffle/react-auth
- npm.io page: https://npm.io/package/@reshuffle/react-auth

## Dependencies (2)

- [@reshuffle/auth](https://npm.io/package/@reshuffle/auth.md) 0.0.1
- [react-async-hook](https://npm.io/package/react-async-hook.md) ^3.6.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

- 0.1.7 (latest) — 2019-11-24
- 0.1.6 — 2019-11-21
- 0.1.5 — 2019-11-21
- 0.1.4 — 2019-11-20
- 0.1.3 — 2019-11-20
- 0.1.2 — 2019-11-19
- 0.1.1 — 2019-11-18

## README

## `@reshuffle/react-auth`
React context for adding "out-of-the-box" auth to your app using `@reshuffle/passport`.

See a fully working demo [here](https://github.com/reshufflehq/auth-template).

### Installation
```console
$ npm install @reshuffle/react-auth
```

### Prerequisites
* Install `@reshuffle/passport`
* Configure `backend/_handler.js` to use `@reshuffle/passport`, see [instructions](../passport/README.md#usage)

### Usage
Wrap your root component with `AuthProvider`.

`src/App.js`
```jsx
import React from 'react';
import { AuthProvider } from '@reshuffle/react-auth';
import Main from './components/Main';

export default function App() {
  return (
    <AuthProvider>
      <Main />
    </AuthProvider>
  );
}
```

Use with react hooks.

`src/components/Main`
```jsx
import React from 'react';
import { useAuth } from '@reshuffle/react-auth';

export default function Main() {
  const {
    loading,
    error,
    authenticated,
    profile,
    getLoginURL,
    getLogoutURL,
    login,
  } = useAuth();

  if (loading) {
    return <div><h2>Loading...</h2></div>;
  }
  if (error) {
    return <div className='error'><h1>{error.toString()}</h1></div>;
  }
  if (!authenticated) {
    return (
      <>
        <a href={getLoginURL()}>Login in same window</a>
        <a href='/login' onClick={(e) => { login({ newWindow: true }); e.preventDefault(); }}>Login from new window</a>
      </>
    );
  }
  return (
    <>
      <img src={profile.picture} />
      <span>{profile.displayName}</span>
      <a href={getLogoutURL()}>Logout</a>
    </>
  );
}
```

Use with react class components.

`src/components/Main`
```jsx
import React from 'react';
import { AuthContext } from '@reshuffle/react-auth';

export default class Main extends React.Component {
  static contextType = AuthContext;

  render() {
    const {
      loading,
      error,
      authenticated,
      profile,
      getLoginURL,
      getLogoutURL,
      login,
    } = this.context;

    if (loading) {
      return <div><h2>Loading...</h2></div>;
    }
    if (error) {
      return <div className='error'><h1>{error.toString()}</h1></div>;
    }
    if (!authenticated) {
      <>
        <a href={getLoginURL()}>Login in same window</a>
        <a href='/login' onClick={(e) => { login({ newWindow: true }); e.preventDefault(); }}>Login from new window</a>
      </>
    }
    return (
      <>
        <img src={profile.picture} />
        <span>{profile.displayName}</span>
        <a href={getLogoutURL()}>Logout</a>
      </>
    );
  }
}
```

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