# authenticated-react-router

> an HOC for managing authenticated routes in react-router

Latest version **2.0.0** (published 2021-01-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install authenticated-react-router
pnpm add authenticated-react-router
yarn add authenticated-react-router
bun add authenticated-react-router
```

## 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.0.0 |
| Published | 2021-01-17 |
| First published | 2019-02-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 197.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Saeed Sheikhi SaeedSheikhi@outlook.com |
| Maintainers | saeedsheikhi |
| Keywords | react, react, router, authenticated, route, authenticate, security |

## Links

- npm: https://www.npmjs.com/package/authenticated-react-router
- Repository: https://github.com/SaeedSheikhi/authenticated-react-router
- Homepage: https://github.com/SaeedSheikhi/authenticated-react-router#readme
- Issues: https://github.com/SaeedSheikhi/authenticated-react-router/issues
- npm.io page: https://npm.io/package/authenticated-react-router

## Dependencies (4)

- [react](https://npm.io/package/react.md) ^17.0.1
- [webpack](https://npm.io/package/webpack.md) ^2.6.1
- [react-dom](https://npm.io/package/react-dom.md) ^17.0.1
- [react-router-dom](https://npm.io/package/react-router-dom.md) ^5.2.0

## 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.0.0 (latest) — 2021-01-17
- 1.2.3 — 2019-02-25
- 1.2.2 — 2019-02-25
- 1.2.1 — 2019-02-22
- 1.2.0 — 2019-02-22
- 1.1.2 — 2019-02-22
- 1.1.1 — 2019-02-22
- 1.1.0 — 2019-02-22
- 1.0.0 — 2019-02-22

## README

# authenticated-react-router

extended components for handling authenticated routes in react-router, manage your private and public routes easily.
just replace Main Route component with AuthenticatedRouter or UnauthenticatedRouter components, see below for installation and use cases.

## Installation

### Yarn

```sh
$ yarn add authenticated-react-router
```

## Usage

### AuthenticatedRoute

only allow authenticated (logged in) users to pass the route, particulary use for private routes such as profile section that need user to be logged in.

#### Props

| Prop            | Description                                                                    |
| --------------- | ------------------------------------------------------------------------------ |
| isAuthenticated | a boolean flag that show user is logged in or not                              |
| redirect        | if user was not logged in redirect user to this location, often is /login page |
| path            | Same as React-Router                                                           |
| component       | Same as React-Router                                                           |

```javascript
import React, { Component } from "react";
import { Route } from "react-router-dom";
import { AuthenticatedRoute } from "authenticated-react-router";
//
import Profile from "./components/Profile";
import Login from "./components/Login";
import Register from "./components/Register";

class App extends Component {
  render() {
    return (
      <section>
        <AuthenticatedRoute
          path="/profile"
          redirect="/login"
          isAuthenticated={this.props.isAuthenticated}
          component={Profile}
        />
        <Route exact path="/search" component={Search} />
      </section>
    );
  }
}

export default App;
```

### UnauthenticatedRoute

force logged in users redirect to pass this route, particularly use for login or register routes that logged in user doesn't need to see.

#### Props

| Prop            | Description                                                                                                                                                                                     |
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| isAuthenticated | a boolean flag that show user is logged in or not                                                                                                                                               |
| redirect        | if user was logged in redirect user to this location, modules will automatically redirect user where he is came but for general purporse you must pass an generic url for example /profile page |
| path            | Same as React-Router                                                                                                                                                                            |
| component       | Same as React-Router                                                                                                                                                                            |

```javascript
import React, { Component } from "react";
import { Route } from "react-router-dom";
import { UnauthenticatedRoute } from "authenticated-react-router";
//
import Profile from "./components/Profile";
import Login from "./components/Login";
import Register from "./components/Register";

class App extends Component {
  render() {
    return (
      <section>
        <UnauthenticatedRoute
          path="/login"
          isAuthenticated={this.props.isAuthenticated}
          redirect="/profile"
          component={Login}
        />
        <UnauthenticatedRoute
          path="/register"
          isAuthenticated={this.props.isAuthenticated}
          redirect="/profile"
          component={Register}
        />
        <Route exact path="/search" component={Search} />
      </section>
    );
  }
}

export default App;
```

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