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

@coopah/microsoft-sso-react

v1.1.2

Published

This package provides a suite of React components designed to integrate Microsoft Identity Platform authentication into your React applications using the Microsoft Authentication Library (MSAL). It simplifies the setup and usage of MSAL to manage user aut

Downloads

46

Readme

@coopah/microsoft-sso-react

This package provides a suite of React components designed to integrate Microsoft Identity Platform authentication into your React applications using the Microsoft Authentication Library (MSAL). It simplifies the setup and usage of MSAL to manage user authentication and session management in a scalable way.

This packages works great together with @coopah/microsoft-sso-node!

Installation

To install the package, run the following command in your project directory:

npm

npm install @coopah/microsoft-sso-react

yarn

yarn add @coopah/microsoft-sso-react

Components

The package includes the following components:

1. RoutesWrapper

A provider component that initializes the MSAL instance with custom configurations and wraps the entire part of an application that requires MSAL context.

Props

| Prop | Type | Required | Default | Description | |--------------------------|-------------|----------|------------------|-----------------------------------------------------------------| | clientId | string | Yes | - | Your application's client ID in Azure AD. | | tenantId | string | Yes | - | Your Azure AD tenant ID. | | redirectUri | string | No | /auth-callback | Redirect URI after authentication. | | postLogoutRedirectUri | string | No | /home | URI to navigate after logout. | | cacheLocation | string | No | sessionStorage | Browser storage to cache tokens. | | storeAuthStateInCookie | boolean | No | false | Whether to store the auth state in cookies. | | children | ReactNode | Yes | - | React components that require MSAL context. |

2. AuthCallbackWrapper

This component wraps any child components that require user authentication. It uses MSAL to handle authentication redirects and ensures that the user is logged in before rendering the children.

Props

| Prop | Type | Required | Default | Description | |-----------------|-------------|----------|----------|-------------------------------------------------------------| | handleLogin | function | Yes | - | Function to execute after a successful login. | | loginRoute | string | No | /login | Redirect URI for login. | | errorRoute | string | No | / | Redirect URI for errors. | | children | ReactNode | Yes | - | Child components to render upon successful authentication. |

3. LoginButton

A button component that triggers the MSAL login process when clicked. It displays a styled button that can be customized to match dark or light themes.

Component Image

Props

| Prop | Type | Required | Default | Description | |-----------------|-------------|----------|----------|-------------------------------------------------------------| | darkMode | boolean | No | true | Indicating whether to use dark mode styling. |

How to use

1. RoutesWrapper

This should be in the file where your routes are located. Probably this will be App.jsx

import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";
import { RoutesWrapper } from "@coopah/microsoft-sso-react";
import Login from "./pages/Login";
import AuthCallback from "./pages/AuthCallback";
import Home from "./pages/Home";

function PrivateRoute() {
  // Custom authentication logic
  const isAuth = true;

  if (!isAuth) return <Navigate to="/login" />;

  return <Outlet />;
}

function PublicRoute() {
  return <Outlet />;
}

function App() {
  //Best to store them in an .env file
  //These are required
  const tenantId = ""
  const clientId = ""

  return (
    <BrowserRouter>
      <RoutesWrapper clientId={clientId} tenantId={tenantId}>
        <Routes>
          <Route element={<PublicRoute />}>
            <Route path="/login" element={<Login />} />
            <Route path="/auth-callback" element={<AuthCallback />} />
          </Route>
          <Route element={<PrivateRoute />}>
            <Route path="/home" element={<Home />} />
          </Route>
        </Routes>
      </RoutesWrapper>
    </BrowserRouter>
  );
}

export default App;

2. AuthCallbackWrapper

AuthCallback Page

import React from "react";
import { AuthCallbackWrapper } from "@coopah/microsoft-sso-react";

export default function AuthCallback() {
  const handleLogin = async (token) => {
    try {
      const response = await axios.post(`/auth/microsoft_login`, {
        token,
      });

      //Handle response
    } catch (error) {
      //Handle error
    }
  };

  //handleLogin prop is required

  return (
    <AuthCallbackWrapper handleLogin={handleLogin}>
      <div>AuthCallback</div>
    </AuthCallbackWrapper>
  );
}

3. LoginButton

Login Page

import { LoginButton } from '@coopah/microsoft-sso-react'

export default function Login() {
  return (
    <div>
        <LoginButton />
    </div>
  )
}