[Huawei Developers] Sherlock F:
More information like this, you can visit HUAWEI Developer Forum​

Article Introduction

The following article describes the easiest way to obtain easy, secure, and efficient mobile phone number registration & sign-in using the AppGallery Auth service.

AppGallery Auth Service Introduction

AppGallery Connect provides a cloud-based auth service and SDKs to help you quickly build a secure and reliable user authentication system for your apps to verify user identity.

The AppGallery Connect auth service supports multiple authentication methods and is seamlessly integrated with other Serverless services to help you secure user data based on simple rules that you have defined.

In this article, we will cover just the mobile number authentication method in Android.

Integrating the Auth Service SDK

Before start using the AppGallery Auth Service, we must first integrate the Auth Service SDK by following the steps below :

Step 1:

Create an app in AppGallery Connect and integrate the AppGallery Connect SDK into your app. For details, please refer to AppGallery Connect Service Getting Started.

Step 2:

Add the Auth Service dependencies in the build.gradle file in the app directory (usually app/build.gradle).

implementation 'com.huawei.agconnect:agconnect-auth:1.4.1.300'

Enabling Auth Service

we must also enable the Auth Service in AppGallery Connect by following the steps below :

Step 1:

Sign in to AppGallery Connect and select My projects.

Step 2:

Find your project from the project list and click the app for which you need to enable Auth Service on the project card.

Step 3:

Go to Build > Auth Service. If it is the first time that you use Authe Service, click Enable now in the upper right corner.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/286/0890212000000000286.20200919025236.66526386435016896172191507489420:50510929012354:2800:001CED7CCEE7BBE712A4277F0B2BE60908350EE11DC3FB4451E4EBB363DCA63E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/286/0890212000000000286.20200919025236.66526386435016896172191507489420:50510929012354:2800:001CED7CCEE7BBE712A4277F0B2BE60908350EE11DC3FB4451E4EBB363DCA63E.png[/IMG]

Step 4:

Click Enable in the row of Mobile number authentication mode to be enabled.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/286/0890212000000000286.20200919025245.25150297908848885747025740618988:50510929012354:2800:5CC10FE65E29B956E8B905868997A9D2C445BF432E935C45FF5180397DC1AE00.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/286/0890212000000000286.20200919025245.25150297908848885747025740618988:50510929012354:2800:5CC10FE65E29B956E8B905868997A9D2C445BF432E935C45FF5180397DC1AE00.png[/IMG]

Step 5: (optional)

Configure the SMS template which supports multilingual configuration: Auth Service > settings > Verification code and notification template settings.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/286/0890212000000000286.20200919030634.78698285272293827988243969276493:50510929012354:2800:27A6DC3BADBF8287BDD2A1C30A83189F6D16F5389DD142537B5C3AC47100EA36.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/286/0890212000000000286.20200919030634.78698285272293827988243969276493:50510929012354:2800:27A6DC3BADBF8287BDD2A1C30A83189F6D16F5389DD142537B5C3AC47100EA36.png[/IMG]

Coding

There're five major client-side methods:

SendOTP.

SignUp.

SignIn.

Get the current user.

SignOut.

SendOTP:

Used to apply for verification code for mobile number +[countryCode][phoneNumber]

private void sendOTP(){
VerifyCodeSettings settings = VerifyCodeSettings.newBuilder()
.action(ACTION_REGISTER_LOGIN) //ACTION_REGISTER_LOGIN/ACTION_RESET_PASSWORD
.sendInterval(30) // Minimum sending interval, which ranges from 30s to 120s.
.locale(Locale.getDefault()) // Optional. It indicates the language for sending a verification code.
.build();
Task<VerifyCodeResult> task = PhoneAuthProvider.requestVerifyCode(countryCode, phoneNumber, settings);
task.addOnSuccessListener(TaskExecutors.uiThread(), new OnSuccessListener<VerifyCodeResult>() {
@Override
public void onSuccess(VerifyCodeResult verifyCodeResult) {
// The verification code application is successful.
Log.i(TAG, "onSuccess: "+verifyCodeResult.toString());

}
}).addOnFailureListener(TaskExecutors.uiThread(), new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i(TAG, "onFailure: ");
}
});
}

SignUp:

Used to register a new user using a mobile number +[countryCode][phoneNumber]

private void signUp(){
PhoneUser phoneUser = new PhoneUser.Builder()
.setCountryCode(countryCode)//ex: 212
.setPhoneNumber(phoneNumber)//ex: 698841421
.setVerifyCode(OTP)//OTP sent via the sendOTP() method
.setPassword(null)
.build();
AGConnectAuth.getInstance().createUser(phoneUser)
.addOnSuccessListener(new OnSuccessListener<SignInResult>() {
@Override
public void onSuccess(SignInResult signInResult) {
Log.i(TAG, "onSuccess SignUp: "+signInResult.toString());
// The user is registered and you can add any logic you want
// After that, the user has signed in by default.
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i(TAG, "onFailure SignUp: "+e.getMessage());
// there's an issue (the user already registered ...)
}
});
}

Note 1: Before registering the User, he must obtain the OTP using the sendOTP() method above.

Note 2: After the registration is successful, the user signs in automatically. no need to call the SignIn method.

Note 3: If the user is already registered, the onFailureListener will be triggered.

SignIn:

Used to sign-in the user after obtaining his credential from the AppGallery Connect server.

private void SignIn(){
AGConnectAuthCredential credential = PhoneAuthProvider.credentialWithVerifyCode(countryCode, phoneNumber, null , OTP);
AGConnectAuth.getInstance().signIn(credential)
.addOnSuccessListener(new OnSuccessListener<SignInResult>() {
@Override
public void onSuccess(SignInResult signInResult) {
// Obtain sign-in information.
Log.i(TAG, "onSuccess: login"+signInResult.toString());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i(TAG, "onFailure: login"+e.getMessage());
}
});
}

Note: Before logging in, the user must obtain the OTP using the sendOTP() method above.

Get the current user:

This method is used to get the currently signed-in user.

private AGConnectUser getCurrentUser(){
return AGConnectAuth.getInstance().getCurrentUser();
}

SignOut:

This method is used to sign-out the currently signed-in user.

private void signOut(){
if(getCurrentUser()!=null)
AGConnectAuth.getInstance().signOut();
}

References

AppGallery Auth Service:

https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-auth-service-introduction

Conclusion

Integrating the AppGallery Auth service is the right choice to allow users to use mobile phone numbers to connect to your application securely, easily, and efficiently, without wasting much time and effort building the Auth functionality from scratch.