1.1.0 ā€¢ Published 5 months ago

react-auth-utils v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

react-auth-utils

npm version npm downloads 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:

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.

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:

PropertyDescription
isAuthenticatedA boolean indicating whether the user is authenticated.
userThe user object containing user information (optional, may be null if not authenticated).
tokenThe authentication token (optional, may be null if not authenticated).
signInA function to sign in the user.
signOutA function to sign out the user.

Sign In

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.

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

Contributing

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

1.1.0

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.5

5 months ago

0.0.4

5 months ago

0.0.1

5 months ago