@eduenano27/react-auth-hook
TypeScript icon, indicating that this package has built-in type declarations

1.1.3 • Public • Published

React Auth Hook

Install

npm install @eduenano27/react-auth-hook

AuthProvider<T> Attributes

Attribute Type Description Default
user <T> User data undefined
views IAuthViews Hook views (internal usage, definition only) -/-
hasUser boolean Hook has user false
hasToken boolean Hook has token false
handleLogin (props: { token: string, user: T, permissions: string[] }) => void Set hook data -/-
handleLogout () => void Reset hook data -/-
hasPermission (...permissions: string[]) => boolean User has permission -/-
hasAnyPermission (...permissions: string[]) => boolean User has any permission -/-

IAuthViews Attributes

Attribute Type Description Default
login JSX.Element Component to render when user is not allowed undefined
dashboard JSX.Element Component to render when has not permission / guest is not allowed undefined
loading JSX.Element Component to render when user data is loading undefined

Example

import React from "react";
import { AuthProvider } from "@eduenano27/react-auth-hook";

export default function AppComponent() {
    return (
        <AuthProvider
            api={ 'auth/user' }
        
            views={{
                loading:    <LoadingComponent/>,

                login:      <Navigate to={ 'login' }/>,
                dashboard:  <Navigate to={ 'dashboard' }/>
            }}
        >
            ...
        </AuthProvider>
    );
}

Dependents (0)

Package Sidebar

Install

npm i @eduenano27/react-auth-hook

Weekly Downloads

0

Version

1.1.3

License

ISC

Unpacked Size

11.8 kB

Total Files

22

Last publish

Collaborators

  • eduenano27