# react-auth-utils

> Simple and lightweight React authentication library for seamless user authentication in your projects.

Latest version **1.1.0** (published 2023-12-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-auth-utils
pnpm add react-auth-utils
yarn add react-auth-utils
bun add react-auth-utils
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2023-12-14 |
| First published | 2023-12-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 10.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Imasha Weerakoon \| imasha98.we@gmail.com |
| Maintainers | imwe |
| Keywords | react, authentication, auth, utils, react-auth, security, authorization, frontend, web, authentication-utils, jwt |

## Links

- npm: https://www.npmjs.com/package/react-auth-utils
- Repository: https://github.com/imashaWe/react-auth-utils
- Issues: https://github.com/imashaWe/react-auth-utils/issues
- npm.io page: https://npm.io/package/react-auth-utils

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^18.2.0
- [tslib](https://npm.io/package/tslib.md) ^2.3.0

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2023-12-14
- 1.0.2 — 2023-12-10
- 1.0.1 — 2023-12-10
- 1.0.0 — 2023-12-09
- 0.0.5 — 2023-12-09
- 0.0.4 — 2023-12-09
- 0.0.3 — 2023-12-08
- 0.0.2 — 2023-12-08
- 0.0.1 — 2023-12-06

## README

# react-auth-utils

[![npm version](https://img.shields.io/npm/v/react-auth-utils.svg)](https://www.npmjs.com/package/react-auth-utils)
[![npm downloads](https://img.shields.io/npm/dm/react-auth-utils.svg)](https://www.npmjs.com/package/react-auth-utils)
[![License](https://img.shields.io/npm/l/react-auth-utils.svg)](https://github.com/your-username/react-auth-utils/blob/main/LICENSE)

🔐 Simple and lightweight React authentication library for seamless user authentication in your projects.

## Features

- Easy integration with React applications
- Authentication state management
- Protected route components
- Token handling utilities

## Installation

You can install `react-auth-utils` using npm or yarn:

```bash
npm install react-auth-utils
# or
yarn add react-auth-utils
```

## Usage

### AuthProvider

Wrap your application with the AuthProvider to provide authentication state to your components.

```ts
import { AuthProvider } from 'react-auth-utils';

function App() {
  return <AuthProvider>{/* Your application components */}</AuthProvider>;
}
```

### useAuth

Use the useAuth hook to access authentication state in your components. The useAuth hook provides the following properties:

| Property          | Description                                                                               |
| ----------------- | ----------------------------------------------------------------------------------------- |
| `isAuthenticated` | A boolean indicating whether the user is authenticated.                                   |
| `user`            | The user object containing user information (optional, may be null if not authenticated). |
| `token`           | The authentication token (optional, may be null if not authenticated).                    |
| `signIn`          | A function to sign in the user.                                                           |
| `signOut`         | A function to sign out the user.                                                          |

#### Sign In

```ts
const { signIn } = useAuth();

const fakeJWT = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.';
const expireIn2MinFromNow = Date.now() + 2 * 60 * 1000;
const fakeUser = {
  id: 1,
  username: 'admin',
  email: 'admin@admin.com',
};

signIn(fakeJWT, expireIn2MinFromNow, fakeUser);
```

### ProtectedRoute

Use the RequireAuth component to protect routes that require authentication.

```js
<RequireAuth unauthenticated={<Navigate to="/login" replace={true} />}>
  <ProtectedHomePage />
</RequireAuth>
```

### Contributing

We welcome contributions! Feel free to submit issues and pull requests.

<!-- ### License

This project is licensed under the MIT License - see the LICENSE file for details. -->

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