Huawei Auth Service Integration in HarmonyOS

Mehmet Yozgatlı
Huawei Developers
Published in
3 min readDec 17, 2021

--

Auth Service in HarmonyOS

Introduction

Hello everyone. In this article, I will tell you about a Huawei service that we have heard a lot about before. As new, I will try to explain more about how to integrate this service in HarmonyOS.

First of all, I want to talk a little bit about Auth Service.

  • What is Auth Service?

Most apps need to identify and authenticate users to tailor the app experience for individual users. However, building an authentication system from scratch is not that simple. Auth Service lets you quickly build a secure and reliable user authentication system for your app by integrating Auth Service capabilities, without needing to care about the facilities and implementations on the cloud.

Implementation

The first step is to make the necessary configurations.

  • Create HarmonyOS Project in AGC
  • Enabling Auth Service
  • Integrating SDKs

For details, you can review the document below.

Since Auth Service is an AppGallery Connect SDK, we need to make the necessary configurations there. For details, you can review the document below.

The configuration parts were like this. Now you can move on to coding.

First, you need to choose the type of Authentication that you will use in your project. I wanted to manage this process with Mobile Number in my project.

I used 3 pages while doing this demo project. Register Page, Login Page and Detail Page. I want to share the code samples in order.

1- Register Page

On the register page, there are 2 processes. The first is to send the verifyCode using the user’s phone number. The second is to register a user using phone number, password and verifyCode.

When the registration process is successful, I directed the user’s directly to the detail page, since the signIn process is automatic.

Register Page

2- Login Page

On the login page, I created the “AGConnectAuthCredential” object of the Auth Service SDK by taking the user’s phone and password information. I tried to get the result by giving this object as a parameter to the “signIn” method. If the result is successful, I directed the user to the detail screen.

Login Page

3- Detail Page

On the detail page, there are 3 processes. Setting data when the detail page open, updating user information and signOut. Normally, when users register with their Mobile Number, you can only reach this number.

If you need the user’s name and photo, you should use the “updateProfile” method. Since it is a demo project, I set it hardcode. But you can turn it into a dynamic structure.

Detail Page

Output

1- Register Process

Register Process Screen Recording

2- Login Process

Login Process Screnn Recording

Conclusion

You can easily manage your Authentication process in the HarmonyOS ecosystem. As device compatibility, I can say that it will work more regularly on phones and tablets. But as you can see, it can also work on Watch. Huawei Services exclusive to HarmonyOS continue to be developed and updated. It is expected to run smoothly on all devices.

References

Official HarmonyOS Repo :

Official Document :

--

--