@azure/msal-react

中文文档
5.8.1发布于 2026/11/07MITTypeScriptESM + CJS零依赖近 30 天下载 604.6万

Microsoft Authentication Library for React

原始使用文档
文档目录15 节
  1. About
  2. Prerequisites
  3. Version Support
  4. Installation
  5. Build and Test
  6. Building the package locally
  7. Running Tests
  8. Usage
  9. MSAL Basics
  10. Advanced Topics
  11. MSAL React Specific Concepts
  12. Samples
  13. Security Reporting
  14. License
  15. We Value and Adhere to the Microsoft Open Source Code of Co…

Microsoft Authentication Library for React (msal-react)

npm version npm version codecov

Getting StartedAAD DocsLibrary ReferenceSamples
  1. About
  2. FAQ
  3. Changelog
  4. Prerequisites
  5. Version Support
  6. Installation
  7. Build and Test
  8. Usage
  9. Samples
  10. Security Reporting
  11. License
  12. Code of Conduct

About

The MSAL library for JavaScript enables client-side JavaScript applications to authenticate users using Azure AD work and school accounts (AAD), Microsoft personal accounts (MSA) and social identity providers like Facebook, Google, LinkedIn, Microsoft accounts, etc. through Azure AD B2C service. It also enables your app to get tokens to access Microsoft Cloud services such as Microsoft Graph.

The @azure/msal-react package described by the code in this folder uses the @azure/msal-browser package as a peer dependency to enable authentication in Javascript Single-Page Applications without backend servers. This version of the library uses the OAuth 2.0 Authorization Code Flow with PKCE. To read more about this protocol, as well as the differences between implicit flow and authorization code flow, see the section in the @azure/msal-browser readme.

Prerequisites

Version Support

MSAL React versionMSAL support statusSupported React versions
MSAL React v5Active development16 (16.8+), 17, 18, 19
MSAL React v3In maintenance16, 17, 18, 19
MSAL React v1, v2In maintenance16, 17, 18

Note: There have been no functional changes in the MSAL React v2 release.

Installation

The MSAL React package is available on NPM.

npm install react react-dom
npm install @azure/msal-react @azure/msal-browser

Build and Test

See the contributing.md file for more information.

Building the package locally

To build the @azure/msal-react library, you can do the following:

// Install dev dependencies from root of repo
npm install
// Change to the msal-react package directory
cd lib/msal-react/
// To run build only for react package
npm run build

To build both the @azure/msal-react library and @azure/msal-browser libraries, you can do the following:

// Install dev dependencies from root of repo
npm install
// Change to the msal-react package directory
cd lib/msal-react/
// To run build for react and browser packages
npm run build:all

Running Tests

@azure/msal-react uses jest to run unit tests and coverage.

// To run tests
npm test
// To run tests with code coverage
npm run test:coverage

Usage

For help getting started with @azure/msal-react please see our getting started doc.

Migrating from react-aad-msal? Check out our migration guide.

MSAL Basics

Since @azure/msal-react is a wrapper around @azure/msal-browser many docs from the msal-browser repo are relevant here as well. For concepts specific to @azure/msal-react please see below

  1. Initialization
  2. Acquiring and using an access token
  3. Managing token lifetimes
  4. Managing Accounts
  5. Logging out

Advanced Topics

MSAL React Specific Concepts

  1. Hooks
  2. Events
  3. Class Components
  4. Performance

Samples

Our samples directory contains several example apps you can spin up to see how this library can be used in different contexts.

More advanced samples backed with a tutorial can be found in the Azure Samples space on GitHub:

Security Reporting

If you find a security issue with our libraries or services please report it to [email protected] with as much detail as possible. Your submission may be eligible for a bounty through the Microsoft Bounty program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting this page and subscribing to Security Advisory Alerts.

License

Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.

We Value and Adhere to the Microsoft Open Source Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

版本与发布记录

@azure/msal-react 共发布 148 个版本,首次发布于 2020/11/12;过去 12 个月发布了 44 个版本。

版本发布日期距今
5.8.1latest2026/11/07今天
5.8.02026/11/052 天前
5.7.42026/10/2811 天前
5.7.32026/10/2217 天前
5.7.22026/10/031 个月前
5.7.12026/09/161 个月前

其他发布通道:alpha → 5.0.0-alpha.2,beta → 5.0.0-beta.0,lts → 3.0.29。

关于 @azure/msal-react 的常见问题

@azure/msal-react 是什么?

Microsoft Authentication Library for React

如何安装 @azure/msal-react?

在项目目录运行 npm install @azure/msal-react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.8.0 || ^17 || ^18 || ^19.2.1)、@azure/msal-browser(^5.29.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@azure/msal-react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@azure/msal-react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@azure/msal-react 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@azure/msal-react 可以商用吗?

@azure/msal-react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@azure/msal-react 的下载量有多少?

根据 npm 官方下载统计,@azure/msal-react 在 2026/10/08 – 2026/11/06 这 30 天内被下载 6,045,586 次,日均约 201,520 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@azure/msal-react 还在维护吗?

最新版本 5.8.1 发布于 2026/11/07(今天);至今共发布 148 个版本,其中过去 12 个月发布了 44 个;首次发布于 2020/11/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。