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

1.5.8 • Public • Published

Mirats Auth

This project integrates Single Sign-On (SSO) functionality using the @mirats/mirats-auth package. Follow the steps below to set up the SSO in your application.

Installation

First, install the SSO package:

npm i @mirats/mirats-auth

1. Create GlobalContext.js

Create a file called GlobalContext.js inside the root of your app directory with the following content:

"use client";
import ProtectRoute from "../components/ProtectRoute";
import { getAuth } from "@mirats/mirats-auth";
import { createContext, useContext, useEffect, useState } from "react";

const AppContext = createContext(null);
export const useGlobalContext = () => {
  return useContext(AppContext);
};
const GlobalContext = ({ children }) => {
  const [userData, setUserData] = useState({});
  const getUserData = async () => {
    try {
      const user = await getAuth();
      if (user && user?.currentUser?._id) {
        setUserData(user?.currentUser);
        return;
      }
    } catch (error) {
      console.log(error);
    }
  };

  useEffect(() => {
    if (!Object.keys(userData || {})?.length) {
      getUserData();
    }
  }, [userData?._id]);

  return (
    <AppContext.Provider value={{ userData }}>
      <ProtectRoute>
        {children}
      </ProtectRoute>
    </AppContext.Provider>
  );
};
export default GlobalContext;

2. Create ProtectRoute.js

Create a file outside the app directory called ProtectRoute.js with the following content:

"use client";
import { isLoggedIn, signIn } from "@mirats/mirats-auth";
const ProtectRoute = ({ children }) => {
  const { user, loading } = isLoggedIn();
  if (user && !loading) {
    console.log("user & loading", { user, loading });
    return children;
  } else {
    signIn();
    console.log("no user found");
    return null;
  }
};

export default ProtectRoute;

3. Import GlobalContext in layout.js

Import the GlobalContext file inside your layout.js file as shown below:

import dynamic from "next/dynamic";
const GlobalContext = dynamic(() => import("@/context/index"), { ssr: false });
const inter = Inter({ subsets: ["latin"] });

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body className={inter.className}>
        <GlobalContext>
         {children}
        </GlobalContext>
      </body>
    </html>
  );
}

Add Team Member API Documentation

This documentation outlines the process of inviting a user to join a team using the inviteLink function and its associated steps. The goal is to generate an invite link for the user, ensuring they are properly registered and assigned a gasId, then sending the invite through the appropriate backend services.

Function Overview

inviteLink()

 inviteLink(email: string, id: string, firstName?: string, lastName?: string, days: number = 30): string | null

Parameters:

  email:"example@gmail.com"// required,
  id: "" // required,
  firstName: "testers", // optional
  lastName: "test", // optional
  days: 30 // optional(default: expires in 30 days)

This function generates an invitation link for a given user, ensuring they exist in the system. It performs several steps:

-Check if the user exists in the backend system.
-Retrieve or generate a gasId for the user.
-Call the sendUserInvite function to send the invitation.


2. Retrieve or Generate gasId / (getGasId())

  await getGasId(email: string): Promise<{ gasId: string | null } | null>

Request body

For getGasId:
  email:"example@gmail.com"// required,

Response body

 {
  gasId: "data?.gasId"  //mongoose objectId
  email: "data?.email"  //email
 }

3. Retrieve or Generate gasIds( getGasIds())

  await getGasIds([email: string]): Promise<{ gasId: string | null } | null>

Request body

For getGAsIds: (max limit 10)
[
    "example1@gmail.com" ,  # required
    "example2@gmail.com" ,
    "example3@gmail.com" ,
    "example4@gmail.com" ,
    "example5@gmail.com"
]

Response body

 {
  gasId: "data?.gasId"  //mongoose objectId
  email: "data?.email"  //email
 }

4. Sending the Invitation(sendUserInvite())

  await sendUserInvite(body: inviteBody): Promise<void>

Request body

For sendUserInvite:

  type inviteBody
  {
  name: string //req
  email: string, //req
  id: string,    //req
  organizationName: string, //Optional
  invitedBy: string, //req
  days: number  //optional
  }

Response body

  { success: true, msg: "Invite sent successfully", email:"userEmail", id:"mongooseId"  }

Purpose:

  • Sends the invitation to the user based on the information provided.

  • 5. Sending the Invitation(sendUserInvites())

      await sendUserInvite(body: inviteBody): Promise<void>
    

    Request body

    For sendUserInvites: (max limit of 10)
    
      type inviteBody[]
      [
        {
          name: string, // required
          email: string, // required
          id: string,    // required
          organizationName: string, // optional
          invitedBy: string, // required
          days: number  // optional
        },
        {
          name: string, // required
          email: string, // required
          id: string,    // required
          organizationName: string, // optional
          invitedBy: string, // required
          days: number  // optional
        },
        // More inviteBody objects can be added as needed
      ]

    Response body

    [{ success: true, msg: "Invite sent successfully", email:"userEmail", id:"mongooseId"  }]
    
    

    Purpose:

  • Sends the invitation to the user based on the information provided.


  • Workflow Steps

    1. User Existence

    -The function starts by verifying if the provided email corresponds to an existing user in the backend portal.
    -If the user is not found, the system still proceeds to create a gasId for the new user.


    2. Retrieve or Generate gasId or gasIds(getGasId() / getGasIds())

    Input:

  • An email address (string) for which the gasId needs to be retrieved.

    Output:

  • If the user exists: Returns an object containing the gasId.

  • If the user does not exist: Generates a new gasId and returns it.

  • Purpose:

  • The gasId serves as a unique identifier for the user in the system.

  • 3. Sending the Invitation(single and multiple users)(sendUserInvite() / sendUserInvites)

    a. Validation of Input Data

  • Single User:

  • -The function sendUserInvite takes a body object containing user details (e.g., name, email, ID, etc.).
    -It checks that all required fields (name, email, id, and invitedBy) are present and correctly formatted.
    -Optional fields like organizationName and days are validated for type and format if provided.


  • Multiple Users:

  • -The function sendUserInvites accepts an array of inviteBody objects.
    -Each object in the array is validated against the same criteria as for a single user.
    -If any object fails validation, it is excluded from the process, and the system logs or returns the failure reason.

    b. Send the Invitation

  • For Single User:

  • -The sendUserInvite function sends the invitation to the specified email address.
    -If successful, it confirms that the invitation has been sent.

  • For Multiple Users:

  • -The sendUserInvites function iterates through the array of inviteBody objects:
    -Each validated user receives an invitation.
    -Failed invitations (due to missing/invalid data or other issues) are logged or included in a failure response.

    Purpose:

  • Sends the invitation link to the user based on the information provided.

  • Readme

    Keywords

    none

    Package Sidebar

    Install

    npm i @mirats/mirats-auth

    Weekly Downloads

    35

    Version

    1.5.8

    License

    ISC

    Unpacked Size

    26.2 kB

    Total Files

    5

    Last publish

    Collaborators

    • atomostech
    • altaab