1.2.24 • Published 5 months ago

auth-kit-react v1.2.24

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

Auth Kit React

npm version License: MIT

Auth Kit React is a versatile authentication and authorization package for React applications. It provides a simple and configurable solution for managing user authentication, authorization, and session handling.

Features

  • User Authentication: Easily integrate user authentication into your React applications.
  • Authorization: Manage user roles and permissions for secure access control.
  • Session Handling: Handle user sessions and persist authentication state.
  • Configurability: Configure the package based on your application's specific needs.

Table of Contents

Installation

npm install auth-kit-react
or
yarn add auth-kit-react

Usage

  1. Import AuthProvider and useAuth in your main application file.
// src/App.js

import React from 'react';
import { AuthProvider, useAuth, ProtectedRoute } from 'auth-kit-react';

function App() {
  return (
    <AuthProvider authUtilsOptions={{ baseUrl: 'your-api-url' }}>
      {/* Your application components */}
    </AuthProvider>
  );
}

export default App;
  1. Use the useAuth hook in your components.
// src/components/Profile.js

import React from 'react';
import { useAuth } from 'auth-kit-react';

function Profile() {
  const { token, login, logout } = useAuth();

  return (
    <div>
      {token ? (
        <button onClick={logout}>Logout</button>
      ) : (
        <button onClick={() => login('username', 'password')}>Login</button>
      )}
    </div>
  );
}

export default Profile;
  1. Protect your routes using ProtectedRoute component.
// src/components/Dashboard.js

import React from 'react';

function Dashboard() {
  return <div>Protected Dashboard</div>;
}

export default Dashboard;
  1. Add protected routes in your main application.
// src/App.js

import React from 'react';
import { Route, Routes } from 'react-router-dom';
import Dashboard from './components/Dashboard';
import Login from './components/Login';
import { ProtectedRoute, AuthProvider } from 'auth-kit-react';

function App() {
  return (
    <AuthProvider authUtilsOptions={authUtilsOptions}>
        <Routes>
          <Route path="/login" element={<Login />} />
          <Route element={<ProtectedRoute />}>
            <Route path="/dashboard" element={<Dashboard />} />
          </Route>
        </Routes>
      </AuthProvider>
  );
}

export default App;

API Documentation

AuthProvider

Props:

  • authUtilsOptions (required): An object containing authentication utility options like baseUrl.

useAuth Hook

Returns:

An object with the following properties:

  • token: The current authentication token.
  • login: A function to perform login.
  • logout: A function to perform logout.
  • useAuthenticatedApi: A function to use the authenticated API.

ProtectedRoute Component

Props:

  • path (required): The route path.
  • element (required): The component to render for the protected route.

License

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

1.2.24

5 months ago

1.2.23

5 months ago

1.2.22

5 months ago

1.2.21

5 months ago

1.2.20

5 months ago

1.2.19

5 months ago

1.2.18

5 months ago

1.2.17

5 months ago

1.1.17

5 months ago

1.2.16

5 months ago

1.2.15

5 months ago

1.2.14

5 months ago

1.2.13

5 months ago

1.2.12

5 months ago

1.2.11

5 months ago

1.2.10

5 months ago

1.2.9

5 months ago

1.2.8

5 months ago

1.2.7

5 months ago

1.2.6

5 months ago

1.2.5

5 months ago

1.2.4

5 months ago

1.2.3

5 months ago

1.2.2

5 months ago

1.2.1

5 months ago

1.2.0

5 months ago

1.1.0

5 months ago

1.0.0

5 months ago