[Huawei Developers] HMS Community:
View attachment 5632187

Introduction

In this article, I will be integrating Huawei Account Kit in a Travel application. 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

1. Any operating system (MacOS, Linux and Windows).

2. Must have a Huawei phone with HMS 4.0.2.300 or later.

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

4. Minimum API Level 21 is required.

5. Required EMUI 9.0.0 and later version devices.

Integrate HMS Dependencies

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

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

3. Generate a SHA-256 certificate fingerprint.

4. To generate SHA-256 certificate fingerprint. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code.

Or use cmd as explained in SHA256 CODE

5. Create an App in AppGallery Connect.

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

View attachment 5632191

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

View attachment 5632193

8. Click Manage APIs tab and enable Account Kit.

React Native Project Preparation

9. 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

10. Create project using below command.

react-native init project name

11. 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”

12. Configure android level build.gradle

Add to buildscript/repositories and allprojects/repositories

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

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

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

14. Linking the HMS Account Kit Sdk.

Run below command in the project directory

react-native link react-native-hms-account

15. Add below permissions to Android.manifest file.

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

Development

Sign in and sign out (Without Auth Button)

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);
});

Sign Out

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

Sign in using HMSAuthButton

import HMSAccount, { HMSAccountAuthService, HMSAuthParamConstants, HMSAuthScopeListConstants, HMSAuthRequestOptionConstants } from "@hmscore/react-native-hms-account";
signIn = () => {
let signInData = {
accountAuthParams: HMSAuthParamConstants.DEFAULT_AUTH_REQUEST_PARAM,
authRequestOption: [HMSAuthRequestOptionConstants.ID_TOKEN, HMSAuthRequestOptionConstants.ACCESS_TOKEN],
authScopeList: [HMSAuthScopeListConstants.EMAIL]
};

HMSAccountAuthService.signIn(signInData)
.then((response) => { console.log(response) })
.catch((err) => { console.log(err) });
};
...
<HMSAuthButton
style={styles.viewcontainer}
colorPolicy={
HMSAccount.HUAWEI_ID_AUTH_BUTTON_COLOR_POLICY_RED
}
enabled={true}
theme={HMSAccount.HUAWEI_ID_AUTH_BUTTON_THEME_FULL_TITLE}
cornerRadius={
HMSAccount.HUAWEI_ID_AUTH_BUTTON_CORNER_RADIUS_MEDIUM
}
onPress={signIn}
/>

Testing

Run the android App using the below command.

react-native run-android

Result

View attachment 5632197

Tips and Tricks

1. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.

2. Make sure you have added the agconnect-services.json file to app folder.

3. Make sure you have added SHA-256 fingerprint without fail.

4. Make sure all the dependencies are added properly.

5. For project cleaning navigate to android directory and run the below command.

gradlew clean

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