DEV Community

HMS Community
HMS Community

Posted on

Guideline to Integrate Huawei ID button to Sign in (Account Kit) using React Native

Image description

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

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

  • Enter SHA-256 certificate fingerprint and click Save.

  • Click Manage APIs tab and enable Account Kit.

React Native Project Preparation

  • Environment set up, refer below link.
    https://reactnative.dev/docs/environment-setup

  • 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

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);
  });
Enter fullscreen mode Exit fullscreen mode

Sign Out

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

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}
  />
Enter fullscreen mode Exit fullscreen mode

Testing

Run the android App using the below command.
react-native run-android

Result

Image description

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

Top comments (0)