Flutter | Huawei AGC Auth Service (Email Address Authorization)

Ibrahim R. Serpici
Huawei Developers
Published in
4 min readNov 27, 2020
AGC Auth Service Flutter

In this article, I will explain how you can integrate Huawei AGC Auth Service in your Flutter (Android) applications.

What is Auth Service?

Nowadays most of the applications needs to register/ authenticate users to provide them more sophisticated user experience. But unfortunately, building an authentication system from scratch is a costly operation. This is when AGC Auth service comes in handy. Hence it lets developers create a secure & reliable user auth system without worrying about backend/cloud integration since it provides SDKs and backend services by itself.

To explain more clearly, I have prepared an example application and user Auth Service with E Mail Authorization. Here is step by step guide to how to implement auth service below. (Also you can find GitHub link of the demo project in the resources section)

Scenario of the Application

The application will have two separate screen, one for login and one for registering the user.

Users can register with their own e mail by getting verified with Huawei Auth Service, if the registration is successful, then the user can successfully login.

How to Implement?

First of all, HMS Core needs to be implemented to the project. If you do not know how, please refer the project below.

AppGallery Connect Project Settings

In the web console https://developer.huawei.com/ navigate to the Project Settings then Manage APIs to enable Auth Service.

After enabling the Auth Service from Manage APIs, navigate to the Auth Service tab from the left side of the web console and enable Email Address authentication mode.

Dependencies

After integrating HMS Core, the dependencies for auth service needs to be added to the build gradle(app level)

Permissions

Add these permissions below to the your manifest file under android/app/src/profile

After adding permissions & dependencies, finally we are ready to start implementation of the auth service.

Since AGC Auth Service is Android platform specific service, we have to prepare Auth Service methods in Android side first.

To do that, lets create a AuthServiceHelper class right under the app/src/main/kotlin directory.

AuthServiceHelper class will hold our functions required for the authentication process.

AuthServiceHelper class(Platform Specific Android)

In this class, we have methods for logging the user in, registering the user with e mail, requesting a verification code and signing out.

This class is the base class for the auth service. Whenever we need the use auth service, we need to trigger the methods inside of this class to communicate with Huawei Auth Service.

Since this class is works in android side of the application, we need to find a way to interact with the flutter part. This is why we need to build / configure flutter engine to make it communicate with the native android part of the code. To do that, we need to configure our flutter engine in MainActivity.

MainActivity (Android Native)

In MainActivity we are overriding the configureFlutterEngine method to establishing channel between the flutter and the native android part of the application. To set up method gateway we need to activate MethodChannel and setMethodCallHandler

AuthServiceHelper (Dart)

After we set up our MethodChannel and the functions required in main activity, we can finally trigger those methods from Flutter (Dart) part of the application like below.

After setting up AuthServiceHelper class in Flutter(Dart) side, we can use it anywhere in the application to trigger android specific auth service code.

Result:

Getting Verification Code & Registering

Logging in

Resources:

--

--