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

@nait-aits/azure-ad-auth

v0.0.16

Published

A library to make working with Azure AD authentication easier

Downloads

72

Readme

azure-ad-auth

This package helps with the setup of react applications using Azure AD authentication.

This currently does not support B2C, but that will be coming.

Installation

npm install @nait-aits/azure-ad-auth

Requirements

  • @azure/msal-browser
  • @azure/msal-react

Hooks and Providers

Setup

NaitAzureADAuthProvider

You will need to wrap your application (or specific components) in a NaitAzureADAuthProvider. This will apply the configuration across all children.

You will need to provide a clientId and tenantId at a minimum.

App.ts (or app entry point)

import { NaitAzureADAuthProvider } from "@nait-aits/azure-ad-auth";

function App() {
  return (
    <NaitAzureADAuthProvider
      config={{
        clientId: "GUID",
        tenantId: "GUID",
      }}
    >
      <Control />
    </NaitAzureADAuthProvider>
  );
}

Azure B2C

If you are using Azure B2C, there are a few differences.

  1. Do not include the tenant id
  2. Specify signinAuthority
  • will be something like "https://xxx.b2clogin.com/xxx.onmicrosoft.com/signinsignup"
  1. Specify the knownAuthorities
  • Will be something like "xxx.b2clogin.com"

App.ts (or app entry point)

import { NaitAzureADAuthProvider } from "@nait-aits/azure-ad-auth";

function App() {
  return (
    <NaitAzureADAuthProvider
      config={{
        clientId: "GUID",
        signinAuthority: "https://xxx.b2clogin.com/xxx.onmicrosoft.com/signinsignup",
        knownAuthorities: ["xxx.b2clogin.com"],
        defaultScopes: ["https://xxx.onmicrosoft.com/clientId/tasks.read"],
        redirectUri: "http://localhost:3000",
      }}
    >
      <Control />
    </NaitAzureADAuthProvider>
  );
}

Usage

You should now be at a point where everything works. Now what about logging in/out? What is the username? Are they even logged in?

To do this, you just use the azure msal items.

Here is a very simple example page.

import { useIsAuthenticated, useMsal } from "@azure/msal-react";

function SamplePage() {
  var { instance, accounts } = useMsal();
  var isAuthenticated = useIsAuthenticated();

  return (
    <div>
      {isAuthenticated && <div>Hello {accounts[0]?.name}</div>}
      {!isAuthenticated && <div>Please log in.</div>}
      <button
        onClick={() => {
          isAuthenticated && instance.logout();
          !isAuthenticated && instance.loginPopup();
        }}
      >
        Log {isAuthenticated ? "Out" : "In"}
      </button>
    </div>
  );
}

export default SamplePage;

NaitAzureADAuthProvider config (optional)

If you have any overrides, you can specify the configuration here as well

Only need to specify the items you are overriding/need.

For example:

<NaitAzureADAuthProvider
  config={{
    redirectUri: `${window.location.origin}/login-complete`,
  }}
>
  <Control />
</NaitAzureADAuthProvider>

useGetToken

If you ever need to get the token for a user, you can use the useGetToken hook. This will return a token, and perform any token refresh if needed.

import { useGetToken } from "@azure/msal-react";

...

const tokenRetreiver = useGetToken();

var tokenResult = useGetToken();

if( tokenResult)

Debug

If you are having issues, you can enable the debug panel by setting the debug value to true. This will output a div that contains the values that the provider is using.

All Config values

{
    clientId?: string;
    tenantId?: string;
    redirectUri?: string;
    maxLogLevel?: 0 | 1 | 2 | 3 | 4;
    defaultScopes?: string[];
    debug?:boolean;
    //from @azure/msal-browser
    cacheOptions?: CacheOptions;

    //B2C Values
    signinAuthority?:string;
    knownAuthorities?: string[];
}

Integration with @nait-aits/fetch-state or @nait-aits/signalr

if you are using the package @nait-aits/fetch-state or @nait-aits/signalr, you can easily tap into the useGetToken hook and all auth is take care of for you.

import "./App.css";
import TestAuth from "./TestAuth";
import { NaitAzureADAuthProvider, useGetToken } from "@nait-aits/azure-ad-auth";

import { StateLoaderConfigurationProvider } from "@nait-aits/fetch-state";

function App() {
  return (
    <NaitAzureADAuthProvider>
      <StateLoaderConfigurationProvider
        options={{
          getAuthToken: useGetToken,
        }}
      >
        <TestAuth />
      </StateLoaderConfigurationProvider>
    </NaitAzureADAuthProvider>
  );
}

export default App;