[Huawei Developers] Martin Bieber:
Introduction

In this article, we will looking that how Huawei Auth Service-AGC provides secure and reliable user authentication system to your application. However building such systems is very difficult process, using Huawei Auth Service SDK only need to access Auth Service capabilities without implementing on the cloud.

Here I am covering sign in anonymously which is anonymous account sign to access your app as guest when you wish to login anonymously, Auth Service provides you unique id to uniquely identify user. And authenticating mobile number by verifying through OTP.

Overview

You need to install Unity software and I assume that you have prior

knowledge about the unity and C#.

Hardware Requirements

A computer (desktop or laptop) running Windows 10.

A Huawei phone (with the USB cable), which is used for debugging.

Software Requirements

Java JDK 1.7 or later.

Unity software installed.

Visual Studio/Code installed.

HMS Core (APK) 4.X or later.

Integration Preparations

1. Create a project in AppGallery Connect.

2. Create Unity project.

3. Huawei HMS AGC Services to project.

https://assetstore.unity.com/packag...awei-hms-agc-services-176968#version-original

4. Download and save the configuration file.

Add the agconnect-services.json file following directory Assests > Plugins > Android

5. Add the following plugin and dependencies in LaucherTemplate.

1	apply plugin: 'com.huawei.agconnect'

6. Add the following dependencies in MainTemplate.

1	apply plugin: 'com.huawei.agconnect'

1

2

3	implementation 'com.huawei.agconnect:agconnect-auth:1.4.2.301'

implementation 'com.huawei.hms:base:5.2.0.300'

implementation 'com.huawei.hms:hwid:5.2.0.300'

7. Add dependencies in build script repositories and all project repositories & class path in BaseProjectTemplate.

1	maven { url 'https://developer.huawei.com/repo/' }

8. Create Empty Game object rename to GameManager, UI canvas input text fields and buttons and assign onclick events to respective components as shown below.

MainActivity.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96	package com.huawei.AuthServiceDemo22;

import android.content.Intent;

import android.os.Bundle;

import com.hw.unity.Agc.Auth.ThirdPartyLogin.LoginManager;

import com.unity3d.player.UnityPlayerActivity;

import android.util.Log;

import com.huawei.agconnect.auth.AGConnectAuth;

import com.huawei.agconnect.auth.AGConnectAuthCredential;

import com.huawei.agconnect.auth.AGConnectUser;

import com.huawei.agconnect.auth.PhoneAuthProvider;

import com.huawei.agconnect.auth.SignInResult;

import com.huawei.agconnect.auth.VerifyCodeResult;

import com.huawei.agconnect.auth.VerifyCodeSettings;

import com.huawei.hmf.tasks.OnFailureListener;

import com.huawei.hmf.tasks.OnSuccessListener;

import com.huawei.hmf.tasks.Task;

import com.huawei.hmf.tasks.TaskExecutors;

import java.util.Locale;

public class MainActivity extends UnityPlayerActivity {

@override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

LoginManager.getInstance().initialize(this);

Log.d("DDDD"," Inside onCreate ");

}

public static void AnoniomousLogin(){

AGConnectAuth.getInstance().signInAnonymously().addOnSuccessListener(new OnSuccessListener<SignInResult>() {

@override

public void onSuccess(SignInResult signInResult) {

AGConnectUser user = signInResult.getUser();

String uid = user.getUid();

Log.d("DDDD"," Login Anonymous UID : "+uid);

}

}).addOnFailureListener(new OnFailureListener() {

@override

public void onFailure(Exception e) {

Log.d("DDDD"," Inside ERROR "+e.getMessage());

}

});

}

@override

protected void onActivityResult(int requestCode, int resultCode, Intent data)

{

LoginManager.getInstance().onActivityResult(requestCode, resultCode, data);

}

public static void sendVerifCode(String phone) {

VerifyCodeSettings settings = VerifyCodeSettings.newBuilder()

.action(VerifyCodeSettings.ACTION_REGISTER_LOGIN)

.sendInterval(30) // Shortest sending interval, 30–120s

.build();

String countCode = "+91";

String phoneNumber = phone;

if (notEmptyString(countCode) && notEmptyString(phoneNumber)) {

Task<VerifyCodeResult> task = PhoneAuthProvider.requestVerifyCode(countCode, phoneNumber, settings);

task.addOnSuccessListener(TaskExecutors.uiThread(), new OnSuccessListener<VerifyCodeResult>() {

@override

public void onSuccess(VerifyCodeResult verifyCodeResult) {

Log.d("DDDD"," ==>"+verifyCodeResult);

}

}).addOnFailureListener(TaskExecutors.uiThread(), new OnFailureListener() {

@override

public void onFailure(Exception e) {

Log.d("DDDD"," Inside onFailure");

}

});

}

}

static boolean notEmptyString(String string) {

return string != null && !string.isEmpty() && !string.equals("");

}

public static void linkPhone(String verifyCode1,String phone) {

Log.d("DDDD", " verifyCode1 "+verifyCode1);

String phoneNumber = phone;

String countCode = "+91";

String verifyCode = verifyCode1;

Log.e("DDDD", " verifyCode "+verifyCode);

AGConnectAuthCredential credential = PhoneAuthProvider.credentialWithVerifyCode(

countCode,

phoneNumber,

null, // password, can be null

verifyCode);

AGConnectAuth.getInstance().getCurrentUser().link(credential).addOnSuccessListener(new OnSuccessListener<SignInResult>() {

@override

public void onSuccess(SignInResult signInResult) {

String phoneNumber = signInResult.getUser().getPhone();

String uid = signInResult.getUser().getUid();

Log.d("DDDD", "phone number: " + phoneNumber + ", uid: " + uid);

}

}).addOnFailureListener(new OnFailureListener() {

@override

public void onFailure(Exception e) {

Log.e("DDDD", "Login error, please try again, error:" + e.getMessage());

}

});

}

}

GameManager.cs

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35	using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class GameManager : MonoBehaviour

{

public InputField OtpField,inputFieldPhone;

string otp=null,phone="";

// Start is called before the first frame update

void Start()

{

inputFieldPhone.text = "9740424108";

}

public void onClickButton(){

phone = inputFieldPhone.text;

using (AndroidJavaClass javaClass = new AndroidJavaClass("com.huawei.AuthServiceDemo22.MainActivity"))

{

javaClass.CallStatic("sendVerifCode",phone);

}

}

public void LinkPhone(){

otp = OtpField.text;

Debug.Log(" OTP "+otp);

using (AndroidJavaClass javaClass = new AndroidJavaClass("com.huawei.AuthServiceDemo22.MainActivity"))

{

javaClass.CallStatic("linkPhone",otp,phone);

}

}

public void AnoniomousLogin(){

using (AndroidJavaClass javaClass = new AndroidJavaClass("com.huawei.AuthServiceDemo22.MainActivity"))

{

javaClass.CallStatic("AnoniomousLogin");

}

}

}

10. Click to Build apk, choose File > Build settings > Build, to Build and Run, choose File > Build settings > Build And Run.

Result

Tips and Tricks

Add agconnect-services.json file without fail.

Make sure dependencies added in build files.

Make sure that you enabled the Auth Service in AG-Console.

Make sure that you enabled the Authentication mode in Auth Service.

Conclusion

We have learnt integration of Huawei Auth Service-AGC anonymous account login and mobile number verification through OTP in Unity Game development. Conclusion is Auth Service provides secure and reliable user authentication system to your application.

Thank you so much for reading article, hope this article helps you.

Reference

Official documentation service introduction :

Document

developer.huawei.com

Unity Auth Service Manual :

Auth Service (AGC) | HuaweiService | 1.3.4

docs.unity.cn

Auth Service CodeLabs :

https://developer.huawei.com/consumer/en/codelabsPortal/carddetails/AuthenticationService