[Huawei Developers] HMS Community:
View attachment 5561365

Introduction

In this article, we will be integrating Huawei Account Kit in an Application.

Account Kit

Account Kit: Account Kit provides you with simple, secure and quick sign-in and authorization functions. Instead of entering accounts and passwords and waiting for authentication, users can just tap the Sign in with HUAWEI ID button to quickly and securely sign in to your app with their HUAWEI IDs.

React Native

REACT Native helps you create real and exciting mobile apps with the help of JavaScript only, which is supportable for both android and iOS platforms.

Just code once, and the REACT Native apps are available for both iOS and Android platforms which helps to save development time.

React Native is a framework that builds a hierarchy of UI components to build the JavaScript code.

It uses the same fundamental UI building blocks as regular iOS and Android apps.

Requirements

Any operating system (MacOS, Linux and Windows).

Must have a Huawei phone with HMS 4.0.2.300 or later.

Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

Minimum API Level 21 is required.

Required EMUI 9.0.0 and later version devices.

Integrate HMS Dependencies

First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.

Create a project in android studio, refer Creating an Android Studio Project.

Generate a SHA-256 certificate fingerprint.

To generate SHA-256 certificate fingerprint. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code or use cmd as explained in SHA256 CODE

Create an App in AppGallery Connect.

Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.

View attachment 5561375

Enter SHA-256 certificate fingerprint and click Save, as follows.

View attachment 5561377

Click Manage APIs tab and enable Account Kit.

React Native Project Preparation

Environment set up, refer below link.

Get Started with React Native · React Native

React Native allows developers who know React to create native apps. At the same time, native developers can use React Native to gain parity between native platforms by writing common features once.

reactnative.dev

Create project using below command.

react-native init project name

Download the React Native Account Kit SDK and paste it under Node Modules directory of React Native project. If you cannot find node modules run below command under project directory using CLI.

“npm install” & “npm link”

Configure android level build.gradle

Add to buildscript/repositories and allprojects/repositories

maven {url 'http://developer.huawei.com/repo/'}

Configure app level build.gradle. (Add to dependencies)

Implementation project (“:react-native-hms-account”)

Linking the HMS Account Kit Sdk.

Run below command in the project directory

react-native link react-native-hms-account

Add below permissions to Android.manifest file.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Development

Account Kit provides APIs for normal sign-in, silent sign-in, sign-out, and authorization revoking. Create a UI in your Android Studio project. An example is shown in the following figure.

View attachment 5561393

1. signIn()

Sign In securely to the application using Huawei ID.

import HMSAccount, {
HMSAuthParamConstants,
HMSAuthRequestOptionConstants,
} from "react-native-hms-account";

let signInData = {
huaweiIdAuthParams: HMSAuthParamConstants.DEFAULT_AUTH_REQUEST_PARAM,
authRequestOption: [
HMSAuthRequestOptionConstants.ID_TOKEN,
HMSAuthRequestOptionConstants.EMAIL,
],
};
HMSAccount.signIn(signInData)
.then((response) => {
console.log(response);
})
.catch((err) => {
console.log(err);
});

2. signOut()

Signing out securely using the signOut() method.

HMSAccount.signOut()
.then(() => {
console.log("signOut -> Success")
})
.catch((err) => {
console.log(err)
});

3. silentSignIn()

With silent sign-in, users can sign in without using their credentials for consecutive sign-ins

import HMSAccount, { HMSAuthParamConstants } from "react-native-hms-account";

let silentSignInData = {
huaweiIdAuthParams: HMSAuthParamConstants.DEFAULT_AUTH_REQUEST_PARAM,
};
HMSAccount.silentSignIn(silentSignInData)
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err)
});

4. cancelAuthorization()

Revoking authorization is intended to increase security by forcing the users to use sign-in credentials while signing in.

HMSAccount.cancelAuthorization()
.then(() => {
console.log("cancelAuthorization -> Success")
})
.catch((err) => {
console.log(err)
});

5. HMSReadSMSManager

Read SMS manager is a service that listens to verification code SMS events.

import { HMSReadSMSManager } from "react-native-hms-account";
HMSReadSMSManager.smsVerificationCode()
.then((response) => {
console.log(response)
})
.catch((err) => {
console.log(err)
});

Testing

Run the android App using the below command.

react-native run-android

Result

View attachment 5561399

Conclusion

In this article, we have learnt that how to integrate HMS Account Kit using React Native. HMS Toolkit helps us to Connects users from a wide range of devices, including phones, tablets, and smart displays. It serves over 900 million users from 190+ countries and regions worldwide and supports 70+ Languages. It guarantees two-factor authentication, real-time risk prediction, and GDPR compliance.

Reference

Account Kit: Documentation

Account Kit: Training Video