Authing DocsDocuments
Concept
workflow
Guides
Development Integration
Application integration
Concept
workflow
Guides
Development Integration
Application integration
Old Version
Development Integration
  • Single Sign-On (SSO)
  • Login component

    • React
    • Vue
    • Angular
    • Native JavaScript
    • Complete parameter list
  • JavaScript/Node.js

  • Java / Kotlin

  • Python

  • C#

  • PHP

  • Go

  • Ruby
  • Android

  • iOS

  • Flutter

  • React Native
  • WeChat Mini Program
  • WeChat webpage authorization
  • Framework Integration
  • Error code
  1. Development Integration
  2. /
  3. Login component
  4. /
  5. React

¶ React Login Component

Update Time: 2025-05-14 08:32:28
Edit

The Authing login component (Guard) is an embeddable login form that can be configured according to your needs and is recommended for single-page applications. It allows you to easily add various social login methods so that your users can log in seamlessly and have a consistent login experience on different platforms. Guard shields many implementation details of low-level authentication for developers, as well as cumbersome UI development.

Guard can be integrated into your React project in a componentized form, and you can use this component to quickly implement the login authentication process.

¶ Quick start

¶ Installation

$ yarn add @authing/react-ui-components

# OR

$ npm install @authing/react-ui-components --save

¶ Initialization

Initialize in React project:

import React from "react";
import ReactDOM from "react-dom";
import { authingGuard } from "@authing/react-ui-components";
// import css files
import "@authing/react-ui-components/lib/index.min.css";

const App = () => {
  const appId = "AUTHING_APP_ID";
  const onLogin = userInfo => {
    console.log(userInfo);
  };
  return <authingGuard appId={appId} onLogin={onLogin} />;
};

ReactDOM.render(<App />, root);
How to initialize in an HTML file?

¶ Import by CDN

<!-- JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/@authing/react-ui-components"></script>

<!-- CSS -->
<link href="https://cdn.jsdelivr.net/npm/@authing/react-ui-components/lib/index.min.css" rel="stylesheet"></link>

¶ Initialize in Script code block

<script>
  var App = () => {
    const appId = "authing\_APP\_ID";
    const onLogin = userInfo => {
      console.log(userInfo);
    };
    return <authingReactUIComponents.authingGuard appId={appId} />;
  };

  ReactDOM.render(<App />, root);
</script>

¶ Monitor login events

authing-guardComponent Callback function foronLogin` event,which is called when the user successfully logs in, and you can get the user information of the current user here.View the complete list of events.

import { authingGuard } from "@authing/react-ui-components";
import "@authing/react-ui-components/lib/index.min.css";

function App() {
  return (
    <div className="App">
      <authingGuard
        appId="AUTHING_APP_ID"
        onLogin={userInfo => {
          console.log(userInfo);
        }}
      />
    </div>
  );
}
What should we do after understanding user information?

After obtaining the user information, you can get the user's identity credential (the token field ), which you can carry in subsequent requests sent by the client to the backend server token, for axios example :

const axios = require("axios");
axios
  .get({
    url: "https://yourdomain.com/api/v1/your/resources",
    headers: {
      Authorization: "Bearer ID_TOKEN"
    }
  })
  .then(res => {
    // custom codes
  });

The legitimacy of this needs to be checked in the back-end interface to verify the user's identity. For details of the verification method, see Verifying User Identity Credentials (token) . After identifying the user's identity, you may also need to perform permission management on the user to determine whether the user has permission to operate this API.

¶ Add social login

Pass in the socialConnections list in the initialization parameter config to specify the social logins that need to be displayed(all social logins configured by the applicationare displayed by default).

import { authingGuard, SocialConnections } from "@authing/react-ui-components";

function App() {
  return (
    <div className="App">
      <authingGuard
        appId="AUTHING_APP_ID"
        onLogin={userinfo => {
          console.log(userinfo);
        }}
        config={{
          socialConnections: [SocialConnections.Github]
        }}
      />
    </div>
  );
}
View the list of supported social logins and access procedures

Authing currently supports 4 social logins around the world, such as GitHub, Apple, etc. The following is a complete list:

Social Identity ProviderScenesDocs
WeChat QR Code on PCWebDocumentation
WeChat MobileMobile AppDocumentation
WeChat Web PageWechat BrowserDocumentation
Wechat Official Accounts QR CodeWebDocumentation
Mini ProgramMini ProgramDocumentation
Mini Program QR Code on PCWebDocumentation
Pull-up Mini Program on AppMobile AppDocumentation
QQWebDocumentation
WeiboWebDocumentation
GitHubWebDocumentation
FacebookWebDocumentation
TwitterWebDocumentation
GoogleWebDocumentation
Apple (Web)WebDocumentation
Apple (Mobile)Mobile AppDocumentation
AlipayMobile AppDocumentation
SlackWebDocumentation
GiteeWebDocumentation
GitLabWebDocumentation
BaiduWebDocumentation
LinkedInWebDocumentation
NetEase YIDUNMobile AppDocumentation
QingCloudWebDocumentation
InstagramWebDocumentation

¶ Log out

  1. Initialize the [AuthenticationClient](/reference/sdk-for-node/#Use 认证模块)in the project entry file.
import { initAuthClient } from "@authing/react-ui-components";

initAuthClient({
  appId: "AUTHING_APP_ID"
});
  1. Add a button to log out and bind the click event to getAuthClient().logout().
import React from "react";
import { getAuthClient } from "@authing/react-ui-components";

const LogoutButton = () => {
  return <button onClick={() => getAuthClient().logout()}>Log out</button>;
};

export default LogoutButton;

¶ Implement single sign-on

To use Guard for single sign-on, you need to set isSSO to true during initialization and pass in appHost (application domain name):

const guard = new authingGuard("AUTHING_APP_ID", {
  appHost: "https://xxx.authing.cn",
  isSSO: true
});

¶ Use React Hooks

Guard provides useAuthing to facilitate user management.

import React, { useEffect } from "react";
import { useauthing, initAuthClient } from "@authing/react-ui-components";
// Initialize authClient before using useAuthing
initAuthClient({
  appId: "AUTHING_APP_ID"
});

export const useInfo = () => {
  const { authClient } = useauthing();
  const [user, setUser] = useState();

  useEffect(() => {
    authClient.getCurrent().then(user => setUser(user));
  }, []);

  return <div>Username: {user && user.username}</div>;
};

¶ Export authing-js-sdk

The Guard components are packaged based on the authing JavaScript SDK. When you need to perform some more advanced operations (such as managing user-defined data, modifying user information, logging out):

  1. Call initAuthClient to initialize AuthenticationClient. Calling this function multiple times will only initialize it once.
import { initAuthClient } from "@authing/react-ui-components";

initAuthClient({
  appId: "AUTHING_APP_ID"
});
  1. Then use getAuthClient to get the AuthenticationClient instance.
import { getAuthClient } from "@authing/react-ui-components";

const authClient = getAuthClient();
  1. Call the method of the AuthenticationClient instance. For a complete list of methods, please see the AuthenticationClient method list.
authClient.getCurrentUser().then(user => console.log(user));

¶ Complete parameter

The Authing login component (Guard) provides many advanced configurations, such as customizing the UI and using specific login methods. See the complete parameter list.

¶ Event list

Note that in React, event listeners should be named with small camels, such as: onLogin, onLoginError.

Event name

Event Introduction

Event parameter

Event parameter introduction

load

Authing appId authenticate success,loading complete

authClient

AuthenticationClient object,can directly operate login, register,details in authing-js-sdk

load-error

Authing appId authenticate failed,loading failed

error

Error information

login

User login success

user, authClient

user: user information authClient same as before

login-error

User login failed

error

Error information,including information such as missing/illegal fields or server errors

register

User login success

user, authClient

user: user information authClient same as before

register-error

User login failed

error

Error information,including information such as missing/illegal fields or server errors

pwd-email-send

Forgot password email sending success

-

-

pwd-email-send-error

Forgot password email sending failed

error

Error information

pwd-phone-send

Forgot password mobile verification code sending success

-

-

pwd-phone-send-error

Forgot password mobile verification code sending failed

error

Error information

pwd-reset

Reset password success

-

-

pwd-reset-error

Reset password failed

error

Error information

close

guard close event in modal mode

-

-

login-tab-change

Login tab switching event

activeTab

Tab after switching

register-tab-change

Register tab switching event

activeTab

Tab after switching

register-tab-change

Register tab switching event

activeTab

Tab after switching

register-info-completed

Register Supplemental Success Event

user, udfs, authClient

user: user information udfs: Supplementary custom field information

authClient same as before

register-info-completed-error

Register Supplemental Failure Event

error, udfs, authClient

error: error information udfs: Supplementary custom field information

authClient same as before

¶ Privatization deployment

The privatization deploymentscenario needs to specify the GraphQL endpoint of your privatized Authing service(without protocol header and Path).If you are not sure, you can contact the Authing IDaaS service administrator.

import React from "react";
import { authingGuard } from "@authing/react-ui-components";
import "@authing/react-ui-components/lib/index.min.css";

const App = () => {
  const appId = "AUTHING_APP_ID";
  const config = {
    apiHost: "https://core.you-authing-service.com"
  };
  return <authingGuard appId={appId} />;
};

¶ Online experience


¶ Get help

Join us on forum: #authing-chat (opens new window)

Prev: Login component Next: Vue
  • Quick start
  • Use React Hooks
  • Export authing-js-sdk
  • Complete parameter
  • Event list
  • Privatization deployment
  • Online experience
  • Get help

User identity management

Integrated third-party login
Mobile phone number flash check (opens new window)
Universal login form component
Custom authentication process

Enterprise internal management

Single Sign On
Multi-factor Authentication
Authority Management

Developers

Development Document
Framework Integration
Blog (opens new window)
GitHub (opens new window)
Community User Center (opens new window)

Company

400 888 2106
sales@authing.cn
16 / F, Block B, NORTH STAR CENTURY CENTER, Beijing(Total)
room 406, 4th floor, zone B, building 1, No. 200, Tianfu Fifth Street, Chengdu(branch)

Beijing ICP No.19051205-1

© Beijing Steamory Technology Co.