1.0.5 • Published 26 days ago

kunji-react v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
26 days ago

Kunji React Library

Setup Authentication and Authorization in your React application in seconds without any verification! Suitable for MVP, side projects or hackathon apps.

kunji-react

Introduction

Kunji React Library handles kunji authentication and state automatically to simplify authentication and user management in your applications. The library provides two main components: KunjiProvider and useKunji hook, making it easy to integrate authentication features into your React projects.

Also check the backend nodejs library for server: kunji-node

How to register App

Checkout Kunji Official Site for more information.

  1. Login to Kunji Dashboard.
  2. Go to Developer Mode.
  3. Register your app by filling only 3 required fields.
  4. You will see your App ID and Public Key.

Installation

To install Kunji React, you can use npm or yarn:

# Using npm
npm install kunji-react

# Using yarn
yarn add kunji-react

Basic Usage

The basic usage of this application is demonstrated in the following example. In case of advance usage check all the exposed APIs below.

KunjiProvider

Wrap your main application component with KunjiProvider and pass the necessary configuration as props. This makes authentication context available to the rest of your application.

import { KunjiProvider } from 'kunji-react';

const App = () => {
  const kunjiConfig = {
    appId: 'your-app-id',
  };

  return (
    <KunjiProvider config={kunjiConfig}>
      {/* Your application components */}
    </KunjiProvider>
  );
};

useKunji Hook

Use the useKunji hook in any component to access authentication-related data and functions.

import { useKunji } from 'kunji-react';

const MyComponent = () => {
  const {
    user,
    loading,
    initiateAuthentication,
    logout,
    oAxios,
  } = useKunji();

  // Your component logic here

  // After user is logged in accessToken and refreshToken can be taken from useKunji or you can use oAxios which is an instance of axios that will automatically populate accessToken and handle accessToken expiry on API request.

  return 
  (<div>
    {
        user 
        ? 
        <div>
        Hello {user.fullName}
         <button onClick={() => logout()}> Logout </button>
        </div>
        :
        loading ? <div>Authenticating...</div> : <div> <button onClick={() => initiateAuthentication()}> Login </button> </div>

    }
  </div>);
};

Open in CodeSandbox

Configuration

When using KunjiProvider, you need to provide the necessary configuration through the config prop. Here are the available configuration options:

  • appId (required): Your application's unique Application ID.
  • authorizationServerUrl (optional): The URL of kunji authorization server.
  • loginPageUrl (optional): The URL of kunji login page.
  • axiosBaseUrl (optional): The base URL for the Axios instance used by Kunji React.

useKunji (Advance Usage)

The useKunji hook can be used to access all the exposed APIs and state variables. The hook returns an object with the following fields:

FieldTypeDescription
userAuthUser \| nullThe authenticated user object or null if not authenticated.
loadingbooleanAuthentication state when user is authenticating.
appIdstringYour Application ID.
authorizationServerUrlstringThe URL of Kunji authorization server.
loginPageUrlstringThe URL of Kunji login page.
initiateAuthentication() => voidFunction to initiate the authentication process.
logout(config?: { reload?: boolean }) => voidFunction to log the user out.
getAccessToken() => string \| nullFunction to retrieve the access token or null if not available.
getRefreshToken() => string \| nullFunction to retrieve the refresh token or null if not available.
API{}API Service that includes authentication-related API functions.
API.exchangeRefreshToken(refreshToken: string) => Promise<Tokens \| false>Function to exchange a refresh token for new tokens. Returns a Promise with the new tokens or false if unsuccessful.
API.loginWithAuthCode(authCode: string) => Promise<SuccessfulLoginResponse \| false>Function to perform login using an authorization code. Returns a Promise with the login response or false if unsuccessful.
API.getUserProfile() => Promise<AuthUser \| false>Function to retrieve the user profile. Returns a Promise with the user profile or false if unsuccessful.
oAxiosAxiosInstanceAxios instance that internally handles tokens. Use this to do any request that requires accessToken in Authorization Header

User Object

The user object, obtained through the useKunji hook, represents the authenticated user. Its type is KunjiUserI. Here is a breakdown of its fields:

FieldTypeDescription
_idstringUnique identifier for the user.
fullNamestringFull name of the user.
usernamestringUser's username.
picturestringURL of the user's profile picture.
role'NORMAL' \| 'ADMIN'User's role or permission level.
emailstringUser's email address.
isEmailVerifiedbooleanIndicates whether the user's email is verified.
strategies'EMAIL_OTP' \| 'GOOGLE' \| 'FACEBOOK' \| 'APPLE' \| 'MOBILE_OTP' \| 'PASSWORD'Authentication strategies used by the user.

The user object provides essential information about the authenticated user, allowing you to personalize the user experience based on their identity and roles. The AuthUser interface defines the structure of the user object, providing clarity on the available fields and their data types.

Contributions

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

This project is licensed under the MIT License. Feel free to use, modify, and distribute it as needed.

1.0.5

26 days ago

1.0.4

28 days ago

1.0.3

4 months ago

1.0.2

5 months ago

1.0.1

5 months ago