@asgardeo/auth-react

中文文档
5.6.3发布于 2026/11/09Apache-2.0TypeScriptESM + CJS近 30 天下载 6123

Asgardeo Auth React SDK for React Applications.

原始使用文档
文档目录20 节
  1. Table of Content
  2. Introduction
  3. Prerequisite - Register your application in Asgardeo
  4. Getting Started
  5. 1. Installing the Package
  6. 2. Import AuthProvider and Provide Configuration Parameters
  7. Using APIs
  8. Best practices when using APIs
  9. useAuthContext() hook
  10. Add a Login/Logout Button
  11. Show Authenticated User's Information
  12. Add Routing
  13. API Documentation
  14. Sample Apps
  15. Develop
  16. Prerequisites
  17. Installing Dependencies
  18. Contribute
  19. Reporting issues
  20. License

Asgardeo Auth React SDK

[!WARNING] Deprecation Notice

Asgardeo Auth React SDK has been deprecated and is no longer maintained.
It is now replaced by @asgardeo/react, which is now part of the official Asgardeo JavaScript SDK Suite.

Please migrate your projects to the new SDK to receive the latest updates, features, and security improvements.

Builder Stackoverflow Join the chat at https://discord.gg/wso2 License Twitter


Table of Content

Introduction

Asgardeo Auth React SDK allows React applications to use OpenID Connect - OIDC authentication with Asgardeo as the Consumer Identity and Access Management(CIAM) Provider. The SDK supports following capabilities

Prerequisite - Register your application in Asgardeo

  1. Register to Asgardeo and create an organization if you don't already have one. The organization name you choose will be referred to as <org_name> throughout this document.
  2. Register a Single Page Application in Asgardeo to obtain necessary keys to integrate your application with Asgardeo. You will obtain a client_ID from Asgardeo for your application which will need to embed later in your application for the integration.

Getting Started

Follow this guide to integrate Asgardeo to your own React Application. To try out the sample apps, use this guide.

1. Installing the Package

Run the following command to install @asgardeo/auth-react from the npm registry.

npm install @asgardeo/auth-react --save

2. Import AuthProvider and Provide Configuration Parameters

Asgardeo React SDK exposes the AuthProvider component, which helps you easily integrate Asgardeo to your application.

First, import the AuthProvider component from @asgardeo/auth-react. where you applications root component is defined.

Note Typically the root component of a react app is defined in the index.* file.

import { AuthProvider } from "@asgardeo/auth-react";

Then, wrap your root component with the AuthProvider.

import React from "react";
import { AuthProvider } from "@asgardeo/auth-react";

const config = {
     signInRedirectURL: "https://localhost:3000/sign-in",
     signOutRedirectURL: "https://localhost:3000/dashboard",
     clientID: "<client_ID>",
     baseUrl: "https://api.asgardeo.io/t/<org_name>",
     scope: [ "openid","profile" ]
};

export const MyApp = (): ReactElement => {
    return (
        <AuthProvider config={ config }>
            <App />
        </AuthProvider>
    )
}

Note You can refer to the details of AuthProvider config here

Once the root component is wrapped with AuthProvider, useAuthContext() hook can be used anywhere within the application to implement user authentication capabilities in the application.

Using APIs

Best practices when using APIs

Asgardeo Auth React SDK is built on top of Asgardeo Auth SPA SDK, a base library. Hence, almost all the usable APIs from Auth SPA SDK are re-exported from Asgardeo Auth React SDK and you don't need to import dependencies from the base library to your application.

  • The only SDK that should be listed in the app dependencies is @asgardeo/auth-react.
  • Always import APIs from @asgardeo/auth-react.

Warning IDE or Editor auto import may sometimes import certain APIs from @asgardeo/auth-spa, change them back manually.

Click here for Tips: Do's When importing a component from Asgardeo React SDK

DO ✅

import { AsgardeoSPAClient } from "@asgardeo/auth-react";

When including React SDK as a dependency:

DO ✅
// In package.json

dependencies: {
    "@asgardeo/auth-react": "^2.0.0"
}


useAuthContext() hook

The useAuthContext() hook provided by the SDK could be used to implement the necessary authentication functionalities and access the session state that contains information such as a unique identifier for the authenticated user.

Import the useAuthContext() hook from @asgardeo/auth-react.

import { useAuthContext } from "@asgardeo/auth-react";

And then inside your components, you can access the context as follows

const { state, signIn, signOut } = useAuthContext();

Few common methods that you will require when implementing authentication capabilities in your application.

  • signIn - Initiate a login request to Asgardeo, process the response to obtain authentication response.
  • signOut - Logout the user from Asgardeo and clear any authentication data from the SDK storage.
  • isAuthenticated - Check whether there is an authenticated user session. Based on the result you can decide to change the application view/behaviour.
  • getBasicUserInfo - Get authenticated user's basic information from the authentication response.
  • getDecodedIDToken - Get the decoded id_token obtained in the authentication response. From there you can derive more information such as additional user-attributes.
  • getIDToken - Get the id_token (JWT) obtained in the authentication response.
  • getAccessToken - Get the access_token obtained in the authentication response.
  • refreshAccessToken - Get the refresh_token obtained in the authentication response.
  • getHttpClient - Get an HttpClient instance so that the you can make RESTful calls to the backend, where the client will attach the necessary Authorization headers to the request.

The state object will contain attributes such as whether a user is currently logged in, the username of the currently logged-in user etc.

Note You can refer to the detailed API documentation here


Add a Login/Logout Button

You can use the signIn() method from useAuthContext() to easily implement a login button.

<button onClick={ () => signIn() }>Login</button>

Similarly to the above step, we can use the signOut() method from useAuthContext() to implement a logout button.

<button onClick={() => signOut()}>Logout</button>

Clicking on Login button will take the user to Asgardeo login page. Upon successful signIn(), the user will be redirected to the app (based on the specified signInRedirectURL) and the state.isAuthenticated will be set to true.

Clicking on Logout button will sign out the user and will be redirected to signOutRedirectURL and the state.isAuthenticated will be set to false.

Note Instead of buttons you can use the signIn() & signOut() methods from the SDK to implement any preffered user-experience in your application

You can use the state.isAuthenticated attribute to check the authenticated status of the user.


Show Authenticated User's Information

The following code snippet demonstrates the usage of the state object, together with signIn() and signOut() methods from the context.

import React from "react";
import { useAuthContext } from "@asgardeo/auth-react";

function App() {

  const { state, signIn, signOut } = useAuthContext();

  return (
    <div className="App">
      {
        state.isAuthenticated
          ? (
            <div>
              <ul>
                <li>{state.username}</li>
              </ul>

              <button onClick={() => signOut()}>Logout</button>
            </div>
          )
          : <button onClick={() => signIn()}>Login</button>
      }
    </div>
  );
}

export default App;

Add Routing

If your application needs routing, the SDK provides a multiple approaches to secure routes in your application. You can read more about routing capabilities in Asgardeo here.


API Documentation

Additionally to above, Asgardeo offers a wide range of APIs that you can use to integrate and make use of Asgardeo within your React Application. You can refer to a detailed API documentation here.

Sample Apps

Sample React Apps offered by Asgardeo will allow you to take Asgardeo for a spin without having to setup your own application. You can see how to setup sample apps here.

Develop

Prerequisites

  • Node.js (version 10 or above).
  • yarn package manager.

Installing Dependencies

The repository is a mono repository. The SDK repository is found in the lib directory. You can install the dependencies by running the following command at the root.

yarn build

Contribute

Please read Contributing to the Code Base for details on our code of conduct, and the process for submitting pull requests to us.

Reporting issues

We encourage you to report issues, improvements, and feature requests creating Github Issues.

Important: And please be advised that security issues must be reported to security@wso2com, not as GitHub issues, in order to reach the proper audience. We strongly advise following the WSO2 Security Vulnerability Reporting Guidelines when reporting the security issues.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

版本与发布记录

@asgardeo/auth-react 共发布 91 个版本,首次发布于 2021/01/15;过去 12 个月发布了 6 个版本。

版本发布日期距今
5.6.3latest2026/11/09今天
3.1.2legacy-3.12026/10/2812 天前
5.6.22026/05/155 个月前
5.6.12026/04/286 个月前
5.6.02026/03/267 个月前
5.5.02026/03/127 个月前

关于 @asgardeo/auth-react 的常见问题

@asgardeo/auth-react 是什么?

Asgardeo Auth React SDK for React Applications.

如何安装 @asgardeo/auth-react?

在项目目录运行 npm install @asgardeo/auth-react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.8)、react-dom(>=16.8)、@babel/runtime-corejs3(^7.11.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@asgardeo/auth-react 支持 TypeScript 吗?

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

@asgardeo/auth-react 支持 ESM 还是 CommonJS?

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

@asgardeo/auth-react 可以商用吗?

@asgardeo/auth-react 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@asgardeo/auth-react 的下载量有多少?

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

@asgardeo/auth-react 还在维护吗?

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