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

ad-b2c-react

v1.1.0

Published

React wrapper over MS Authentication library for Azure AD B2C

Downloads

17

Readme

README

Azure AD B2C is a identity provider covering social and enterprise logins.

The Microsoft Authentication library (MSAL) is JavaScript library to authenticate enterprise users using Microsoft Azure Active Directory (AAD), Microsoft account users (MSA), users using social identity providers like Facebook, Google, LinkedIn etc. and get access to Microsoft Cloud or Microsoft Graph.

This module is React wrapper over MSAL for Azure AD B2C. It fully supports active SSO sessions.

Installation

If you are using npm:

npm install @kdpw/msal-b2c-react --save

Or if you are using yarn:

yarn add @kdpw/msal-b2c-react

Initializing the Library

You'll first need to load the module and pass some configuration to the library. Normally this would go in your index.js file:

import authentication from '@kdpw/msal-b2c-react';

authentication.initialize({
    // you can user your b2clogin.com domain here, setting is optional, will default to this
    instance: 'https://login.microsoftonline.com/tfp/', 
    // your B2C tenant, you can also user tenants GUID here
    tenant: 'myb2ctenant.onmicrosoft.com',
    // the policy to use to sign in, can also be a sign up or sign in policy
    signInPolicy: 'mysigninpolicy',
    // the policy to use for password reset
    resetPolicy: 'mypasswordresetpolicy',
    // the the B2C application you want to authenticate with (that's just a random GUID - get yours from the portal)
    applicationId: '75ee2b43-ad2c-4366-9b8f-84b7d19d776e',
    // where MSAL will store state - localStorage or sessionStorage
    cacheLocation: 'sessionStorage',
    // the scopes you want included in the access token
    scopes: ['https://myb2ctenant.onmicrosoft.com/management/admin'],
    // optional, the redirect URI - if not specified MSAL will pick up the location from window.href
    redirectUri: 'http://localhost:3000',
    // optional, the URI to redirect to after logout
    postLogoutRedirectUri: 'http://myapp.com',
    // optional, default to true, set to false if you change instance
    validateAuthority: false,
    // optional, default to false, set to true if you only want to acquire token silently and avoid redirections to login page
    silentLoginOnly: false
});

Authenticating When The App Starts

If you want to set things up so that a user is authenticated as soon as they hit your app (for example if you've got a link to an app from a landing page) then, in index.js, wrap the lines of code that launch the React app with the authentication.run function:

authentication.run(() => {
  ReactDOM.render(<App />, document.getElementById('root'));
  registerServiceWorker();  
});

Triggering Authentication Based on Components Mounting (and routing)

If you want to set things up so that a user is authenticated as they visit a part of the application that requires authentication then the appropriate components can be wrapped inside higher order components that will handle the authentication process. This is done using the authentication.required function, normally in conjunction with a router. The example below shows this using the popular react-router:

import React, { Component } from 'react';
import authentication from '@kdpw/msal-b2c-react'
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import HomePage from './Homepage'
import MembersArea from './MembersArea'

class App extends Component {
  render() {
    return (
      <Router basename={process.env.PUBLIC_URL}>
        <Switch>
          <Route exact path="/" component={HomePage} />
          <Route exact path="/membersArea" component={authentication.required(MembersArea)}>
        </Switch>
      </Router>
    );
  }
}

Getting the Id Token

Simply call the method getIdToken:

import authentication from '@kdpw/msal-b2c-react'

// ...

const id_token = authentication.getIdToken();

Getting the Access Token

Simply call the method getAccessToken:

import authentication from '@kdpw/msal-b2c-react'

// ...

const access_token = authentication.getAccessToken();

Getting the User Name

Simply call the method getUserName:

import authentication from '@kdpw/msal-b2c-react'

// ...

const userName = authentication.getUserName();

Signing Out

To sign out:

import authentication from '@kdpw/msal-b2c-react'

// ...

authentication.signOut();

Thanks

To build this I made fork of react-azure-adb2c module. Thanks!