📜  npm@azure msal-browser 2.3.0 - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:44:47.516000             🧑  作者: Mango

npm@azure msal-browser 2.3.0 - Shell-Bash

Introduction

npm@azure msal-browser 2.3.0 is a library for obtaining and handling authentication tokens for Azure Active Directory (AAD) applications, specifically for browser-based applications. It is built on top of the Microsoft Authentication Library (MSAL) for JavaScript and provides an easy-to-use interface for authentication and authorization.

Features
  • Easy integration with Azure Active Directory
  • Token acquisition and management for accessing Azure AD-protected resources
  • User authentication with the various identity providers supported by AAD
  • Token caching and renewal for seamless user experience
Installation

To install msal-browser, you must have Node.js installed on your system. Once you have Node.js installed, you can use npm to install msal-browser by running the following command:

npm install @azure/msal-browser@2.3.0
Usage

To use msal-browser, you must create a new instance of the PublicClientApplication class. This class requires a configuration object that contains information about your Azure Active Directory app.

import { PublicClientApplication } from "@azure/msal-browser";

const msalConfig = {
    auth: {
        clientId: "<YOUR_CLIENT_ID>",
        authority: "https://login.microsoftonline.com/<YOUR_TENANT_ID>",
        redirectUri: "<YOUR_REDIRECT_URI>"
    }
};

const msalInstance = new PublicClientApplication(msalConfig);

Once you have an instance of PublicClientApplication, you can use it to acquire tokens for your application. Here's an example of how to acquire a token for accessing Microsoft Graph:

const tokenRequest = {
    scopes: ["https://graph.microsoft.com/.default"]
};

msalInstance.acquireTokenSilent(tokenRequest)
    .then(response => {
        // Use the access token to call Microsoft Graph
        console.log(response.accessToken);
    })
    .catch(error => {
        // Attempt to acquire a new token with interactive login
        msalInstance.acquireTokenPopup(tokenRequest)
            .then(response => {
                // Use the access token to call Microsoft Graph
                console.log(response.accessToken);
            })
            .catch(error => {
                // Handle error
                console.error(error);
            });
    });
Conclusion

npm@azure msal-browser 2.3.0 is a powerful library for handling authentication and authorization in browser-based applications. It provides an easy-to-use interface for acquiring and managing access tokens for Azure Active Directory-protected resources. Its features and ease of use make it an excellent choice for developers looking to add authentication and authorization to their web applications.