Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@azure/msal-react

Package Overview
Dependencies
Maintainers
3
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@azure/msal-react

Microsoft Authentication Library for React

  • 2.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
295K
decreased by-24.71%
Maintainers
3
Weekly downloads
 
Created

What is @azure/msal-react?

@azure/msal-react is a library that provides React components and hooks for integrating with the Microsoft Authentication Library (MSAL). It allows developers to easily add authentication and authorization to their React applications using Microsoft identity platform.

What are @azure/msal-react's main functionalities?

Authentication

This feature allows you to set up authentication in your React application. The code sample demonstrates how to initialize the MSAL provider and use a login popup to authenticate users.

import { MsalProvider, useMsal } from '@azure/msal-react';
import { PublicClientApplication } from '@azure/msal-browser';

const msalInstance = new PublicClientApplication({
  auth: {
    clientId: 'your-client-id',
    authority: 'https://login.microsoftonline.com/your-tenant-id',
    redirectUri: 'http://localhost:3000'
  }
});

function App() {
  return (
    <MsalProvider instance={msalInstance}>
      <YourComponent />
    </MsalProvider>
  );
}

function YourComponent() {
  const { instance, accounts } = useMsal();

  const handleLogin = () => {
    instance.loginPopup().catch(e => {
      console.error(e);
    });
  };

  return (
    <div>
      <button onClick={handleLogin}>Login</button>
      {accounts.length > 0 && <p>Welcome, {accounts[0].username}</p>}
    </div>
  );
}

Authorization

This feature allows you to acquire tokens for accessing protected resources. The code sample demonstrates how to silently acquire an access token for a specific scope.

import { useMsal } from '@azure/msal-react';

function YourComponent() {
  const { instance, accounts } = useMsal();

  const handleGetToken = () => {
    const request = {
      scopes: ['user.read'],
      account: accounts[0]
    };

    instance.acquireTokenSilent(request).then(response => {
      console.log('Access Token:', response.accessToken);
    }).catch(e => {
      console.error(e);
    });
  };

  return (
    <div>
      <button onClick={handleGetToken}>Get Token</button>
    </div>
  );
}

Protected Routes

This feature allows you to protect routes in your React application. The code sample demonstrates how to create a protected route that only authenticated users can access.

import { MsalProvider, useIsAuthenticated } from '@azure/msal-react';
import { PublicClientApplication } from '@azure/msal-browser';
import { BrowserRouter as Router, Route, Redirect } from 'react-router-dom';

const msalInstance = new PublicClientApplication({
  auth: {
    clientId: 'your-client-id',
    authority: 'https://login.microsoftonline.com/your-tenant-id',
    redirectUri: 'http://localhost:3000'
  }
});

function ProtectedRoute({ component: Component, ...rest }) {
  const isAuthenticated = useIsAuthenticated();

  return (
    <Route
      {...rest}
      render={props =>
        isAuthenticated ? (
          <Component {...props} />
        ) : (
          <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
        )
      }
    />
  );
}

function App() {
  return (
    <MsalProvider instance={msalInstance}>
      <Router>
        <ProtectedRoute path='/protected' component={ProtectedComponent} />
      </Router>
    </MsalProvider>
  );
}

Other packages similar to @azure/msal-react

FAQs

Package last updated on 05 Nov 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc