Working with Huawei Account Kit on Flutter Projects

Sinan Aktepe
Huawei Developers
Published in
5 min readAug 17, 2020

--

In this article, we’ll be looking at some of the APIs that Account Kit provides and learn how to use them in our Flutter projects.

Huawei Account Kit

Account Kit provides a simple and quick authorization experience to users. It enables to save time from long authorization periods and it’s two factor authentication process keeps users information safe. Account Kit allows you to connect to the Huawei ecosystem using your Huawei Id from a range of devices, such as mobile phones and tablets. Users can quickly and conveniently sign in to apps with their Huawei Ids after granting an initial access permission.

Configuring The Project

Registering as a Developer

As first, you need to create a Huawei developer account and complete the identity verification. For details, please refer to Registering a Huawei Id.

Creating an App

  • Sign in AppGallery Connect and create a new project.
  • Add a new app to your project by clicking the Add App button.

Generating a Signing Certificate Fingerprint

Signing certificate fingerprints are used to verify the authenticity of an App when it attempts to access an HMS Core service through the HMS Core SDK. So before using the HMS Core service, you must generate a signing certificate fingerprint and configure it in AppGallery Connect.

  • Generate a Signing Certificate by referring here.
  • Then you’ll need to export the SHA256 Fingerprint by using keytool provided by JDK. You can find detailed steps here.

Adding Fingerprint Certificate to AppGallery Connect

  • On the application information page of the project, click to add your SHA256 fingerprint. Then click ✓ to save fingerprint.

Enabling Account Kit Service

On the application information page, click Manage APIs and make sure Account Kit is enabled.

Integrating HMS and Account Plugin to Flutter Project

  • Open pubspec.yaml file of your project and add Huawei Account Plugin as a dependency.
  • Download agconnect-services.json file from application information page.
  • Move the file to the android/app directory with the signing certificate file you’ve created.
  • Open the android/build.gradle file and configure the Maven repository address and agconnect plugin for the HMS Core SDK.
  • Add the AppGallery Connect plugin to your app level build.gradle file.
  • Then add the signing configurations in the same file. Don’t forget that your applicationId must be same as the one you created in AppGallery Connect. Also change your minSdkVersion to 19.

Now we’re done with the integration part and ready to use the Account Plugin in our application.

Using Huawei Account Kit Flutter Plugin

Setting Up The Authorization

Account Plugin allows you to customize the authorization. You can request users to authorize their email addresses, profile information or access tokens. To accomplish that, you can use HmsAuthParamHelper class.

  • Create an instance of HmsAuthParamHelper class. Then set the parameters you want to customize.

Signing In

Now we’re going to call signIn method through HmsAuthService class and pass our helper instance to it. In return, this method gives us the Huawei Id information through HmsAuthHuaweiId class.

As this method is triggered, an authorization screen shows up. After clicking the login button, you can see the Huawei Id information is received.

Signing Out

Account Plugin has signOut method which clears the user’s Huawei Id information when it comes to sign out from an app. But the information is not permanently deleted.

Revoking Authorization

After signing in for the first time, when users try to sign in again, the authorization screen will not show up unless users revoke it. Huawei Id information will be received directly. Once the authorization is revoked, logged id information will be deleted. And the authorization screen will be shown on another login attempt.

Sms Verification

One of the options that Account Plugin provides us is the sms verification. This service can catch sms messages in certain formats. Unlike the other authorization methods, you should be able to send sms messages for this special service.

Obtaining Application’s Hash Code

To catch upcoming sms messages, we need to know the hash code value which is unique in each app. Account Plugin provides obtainHashcode method to get that.

Sending Sms Messages

The messages you’ll send should be as follows:

prefix_flag short message verification code is ****** hash_code
  • prefix_flag indicates the prefix of an SMS message, which can be <#>, [#], or \u200b\u200b. \u200b\u200b are invisible Unicode characters.
  • short message verification code is indicates the content of an SMS message, which is user-defined.
  • ****** indicates the verification code.

Receiving Sms Messages

Account Plugin comes with smsVerification method to listen and catch the right formatted messages. Once it is called, the app starts listening. Waits for the messages for five minutes before timing out. Returns the message or error code depending on the situation.

It’s all done. Now you’re ready to use Account Plugin in your Flutter applications.

Conclusion

Huawei Account Kit is such an easy and secure way to carry out authorization processes, along many other kits with powerful solutions. I think these services will keep providing a nice experience for both developers and users as they get bigger and better.

References

--

--