npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-auth-system

v0.0.7

Published

**# ReactAuthSystem**

Downloads

5

Readme

# ReactAuthSystem

Easily manage authentication and authorization in your React applications.

Features:

  • Clear route separation for guest and protected areas.
  • Customizable permission-based access control.
  • Flexible structure for integrating your own components.
  • Built with React Router v6 for efficient routing.

Installation:

npm install react-auth-system

Usage:

  1. Import the component:
import ReactAuthSystem from 'react-auth-system';
  1. Define your routes and components:
const urls = {
  DASHBOARD: {
    url: '/',
    permission: 'web dashboard',
    component: <div>Dashboard</div>,
  },
  UN_AUTHORIZED: {
      url: "/unauthorized",
      permission: null,
      component: <div>Unauthorized</div>,
    },
  // ...other routes
};

const permissionList = ['web dashboard', 'ui notification', 'organization list'];
  1. Provide necessary components:
<ReactAuthSystem
    urls={urls}
    permissionList={["web dashboard", "ui notification", "organization list"]}
    guestRouteComponent={
      <GuestRoute
        user={{
          name: "John Doe",
          email: "[email protected]",
        }}
      />
    }
    loginComponent={<Login />}
    protectedRouteComponent={
      <ProtectedRoute
        user={{
          name: "John Doe",
          email: "[email protected]",
        }}
      />
    }
    masterLayoutComponent={<MasterLayout />}
    unAuthorizedComponent={<Unauthorized />}
    notFoundComponent={<NotFound />}
/>

Available Props:

  • urls: An object defining your routes and their associated components and permissions.
  • permissionList: An array of permissions your application uses.
  • loginPath (optional): The path to the login page (defaults to '/login').
  • guestRouteComponent: The component to render for guest routes.
  • loginComponent: The component to render for the login page.
  • protectedRouteComponent: The component to render for protected routes.
  • masterLayoutComponent: The component to wrap protected routes with a layout.
  • unAuthorizedComponent: The component to render when a user lacks permissions.
  • notFoundComponent: The component to render for 404 routes.

Example:

import React from 'react';
import ReactAuthSystem from 'react-auth-system';
// ...other imports

const App = () => {
  // ...define routes and components

  return (
    <ReactAuthSystem
      urls={urls}
      permissionList={permissionList}
      // ...other props
    />
  );
};

export default App;

## Sample Components

The following sample components demonstrate how to integrate your own components with ReactAuthSystem:

### GuestRoute

  • Redirects logged-in users to the protected area (/).
  • Renders nested routes for guests.
  • Don't need to provide Navigate and Outlet tag this will be coming from the child component.
import React from 'react';

const GuestRoute = ({ user, Navigate, Outlet }) => {
  if (user) {
    return <Navigate to="/" replace />;
  } else {
    return <Outlet />;
  }
};

export default GuestRoute;

### ProtectedRoute

  • Redirects unauthenticated users to the login page (/login).
  • Renders nested routes for authenticated users.
  • Don't need to provide Navigate and Outlet tag this will be coming from the child component.
import React from 'react';

const ProtectedRoute = ({ user, Navigate, Outlet }) => {
  return user ? <Outlet /> : <Navigate to="/login" replace />;
};

export default ProtectedRoute;

### MasterLayout

  • Provides a basic layout for protected routes.
  • Can be customized to match your application's design.
  • Don't need to provide Outlet tag this will be coming from the child component.
import React from 'react';

const MasterLayout = ({ Outlet }) => {
  return (
    <div className="">
      <div className="">
        <Outlet />
        <div className=""></div>
        {/* <Footer /> */}
      </div>
    </div>
  );
};

export default MasterLayout;

### Unauthorized

  • Renders a message when a user lacks the required permissions.
  • Don't need to provide Link tag this will be coming from the child component.
import React from 'react';

const Unauthorized = ({ Link }) => {
  return (
    <div className="">
      <h1 className="">401</h1>
      <p className="">Error!</p>
      <p className="">
        Sorry! You have no permission to perform this task.
      </p>
      <Link className="" to={"/"}>
        Go back to home
      </Link>
    </div>
  );
}

export default Unauthorized;

### NotFound

  • Renders a message for 404 routes.
  • Don't need to provide Link tag this will be coming from the child component.
import './NotFound.css';

const NotFound = ({ Link }) => {
  return (
    <div className="page-not-found">
      <h1 className="not-found-title">404</h1>
      <p className="not-found-message">
        Oops! Looks like you've stumbled upon a page that doesn't exist.
      </p>
      <Link to="/" className="go-home-button">
        Go back home
      </Link>
    </div>
  );
}

export default NotFound;