Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
react-azure-adb2c
Advanced tools
Azure AD B2C is a cost effective identity provider covering social and enterprise logins but it can be awekward to integrate with - its documentation is currently not great and using it involves rooting around across multiple samples, the ADAL library, and the MSAL library.
That being the case I've focused this package on B2C although with minor changes it could be used more broadly. MSAL itself, which this library wraps, is rather generic but B2C has some specific requirements and I think half of the problem with the documentation is that you end up drifting across B2C and straight AD. I wanted to make things simpler for B2C.
Hopefully this will help people writing React apps. It makes use of MSAL underneath and the core of it (other than protecting routes) will probably work with other frameworks too but I use React at the moment. As it's an SPA my assumption in the library and documentation below is that you ultimately want to get an access token that you can use to call remote APIs. See this Azure AD B2C post here for details on how to set this up on the B2C side.
PRs welcome!
If you are using npm:
npm install react-azure-adb2c --save
Or if you are using yarn:
yarn add react-azure-adb2c
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 'react-azure-adb2c';
authentication.initialize({
// optional, will default to this
instance: 'https://login.microsoftonline.com/tfp/',
// your B2C tenant
tenant: 'myb2ctenant.onmicrosoft.com',
// the policy to use to sign in, can also be a sign up or sign in policy
signInPolicy: 'mysigninpolicy',
// 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'
});
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();
});
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 'react-azure-adb2c'
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>
);
}
}
Simply call the method getAccessToken:
import authentication from 'react-azure-adb2c'
// ...
const token = authentication.getAccessToken();
To sign out:
import authentication from 'react-azure-adb2c'
// ...
authentication.signOut();
To build this I made use of the B2C site, the MSAL library docs, the react-adal source and this React MSAL sample. Thanks!
FAQs
Authentication library for Azure AD B2C and ReactJS
The npm package react-azure-adb2c receives a total of 87 weekly downloads. As such, react-azure-adb2c popularity was classified as not popular.
We found that react-azure-adb2c demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.