This reference project is created for that to show features HMS Auth Service. In this project was used several authentication modes.
- Huawei Account
- Mobile Number
- Email Adress
- Anonymous Account
This Project consists of 2 scenes with HMS Unity Plugin. In the first scene, it contains authentication modes. In the second scene, after the login, it contains the information from the auth service.
Auth Service supports multiple authentication modes, and provides a powerful management console, enabling you to easily develop and manage user authentication.
By integrating the Auth Service SDK into your app, you can easily and quickly provide functions such as registration and sign-in for your users. You can choose to provide your users with one or more of the following authentication modes.
- Mobile number
- Email address
- Third-party accounts
- HUAWEI ID
- HUAWEI GameCenter account
- WeChat account
- QQ account
- Weibo account
- Apple ID
- Google account
- Google Play Games account
- Facebook account
- Twitter account
- Anonymous account
- Self-owned account
Android SDK min 21 Net 4.x
This plugin supports:
- Unity version 2019, 2020 - Developed in master Branch
- Unity version 2018 - Developed in 2.0-2018 Branch
Make sure to download the corresponding unity package for the Unity version you are using from the release section
Please check our wiki page
This is an ongoing project, currently WIP. Feel free to contact us if you'd like to collaborate and use Github issues for any problems you might encounter. We'd try to answer in no more than a working day.
- Register your app at Huawei Developer
- Import the Plugin to your Unity project
- Connect your game with the HMS Kit Managers
1.1- Register at Huawei Developer
During this step, you will create an app in AppGallery Connect (AGC) of HUAWEI Developer. When creating the app, you will need to enter the app name, app category, default language, and signing certificate fingerprint. After the app has been created, you will be able to obtain the basic configurations for the app, for example, the app ID and the CPID.
- Sign in to Huawei Developer and click Console.
- Click the under Ecosystem services, click on App Services.
- Click on the AppGallery Connect under Distribution and Promotion.
- Click My apps.
- On the displayed My apps page, click New app on top right.
- Enter the App name, select App category (Game), and select Default language as needed.
- Upon successful app creation, the App information page will automatically display. There you can find the App ID that is assigned by the system to your app.
Set the package name of the created application on the AGC.
- In app information page, there is a label at top saying "My Apps". Mouse hover on it and select My Project. This will lead you to the project information of your application
- You should see a pop up asking about your package name for the application. Select Manually enter a package name
- Fill in the application package name in the input box and click save.
Your package name should end in .huawei in order to release in App Gallery
Create a keystore using Unity or Android Tools. make sure your Unity project uses this keystore under the Build Settings>PlayerSettings>Publishing settings
During this step, you will need to export the SHA-256 fingerprint by using keytool provided by the JDK and signature file.
-
Open the command window or terminal and access the bin directory where the JDK is installed.
-
Run the keytool command in the bin directory to view the signature file and run the command.
keytool -list -v -keystore D:\Android\WorkSpcae\HmsDemo\app\HmsDemo.jks
-
Enter the password of the signature file keystore in the information area. The password is the password used to generate the signature file.
-
Obtain the SHA-256 fingerprint from the result. Save for next step.
During this step, you will configure the generated SHA-256 fingerprint in AppGallery Connect.
- In AppGallery Connect, go to My Project and select your project.
- Go to the App information section, click on + button and enter the SHA-256 fingerprint that you generated earlier.
- Click √ to save the fingerprint.
To import the plugin:
- Download the .unitypackage
- Open your game in Unity
- Choose Assets> Import Package> Custom
- In the file explorer select the downloaded HMS Unity plugin. The Import Unity Package dialog box will appear, with all the items in the package pre-checked, ready to install.
- Select Import and Unity will deploy the Unity plugin into your Assets Folder
In order for the plugin to work, some kits are in need of agconnect-json file. Please download your latest config file from AGC and import into Assets/StreamingAssets folder.
- Click your project for which you need to enable Auth Service from the project list.
- Go to Build > Auth Service. If it is the first time that you use Auth Service, click Enable now in the upper right corner.
- Click Enable in the row of each authentication mode to be enabled.
In order for the plugin to work, you need to select the needed kits Huawei > Kit Settings. In this project , I selected the Account kit and Auth Service.
I selected Account kit also, because i use Huawei Account for authentication mode.
Now you need to call HMSAuthServiceManager as below.
public void Start(){
authServiceManager = HMSAuthServiceManager.Instance;
authServiceManager.OnSignInSuccess = OnAuthSericeSignInSuccess;
authServiceManager.OnSignInFailed = OnAuthSericeSignInFailed;
authServiceManager.OnCreateUserSuccess = OnAuthSericeCreateUserSuccess;
authServiceManager.OnCreateUserFailed = OnAuthSericeCreateUserFailed;
if (authServiceManager.GetCurrentUser() != null) { // skip the login scene }
}
private void OnAuthSericeSignInSuccess(SignInResult signInResult) { }
private void OnAuthSericeSignInFailed(HMSException error) { }
private void OnAuthSericeCreateUserSuccess(SignInResult signInResult) { }
private void OnAuthSericeCreateUserFailed(HMSException error) { }
Now HMS Auth Service is ready to use. Lets look at the authentication modes.
First you need to sign in with Huawei Account Kit and take the access token. And then call AuthService SignIn method and pass the credential parameter with this method.
private void OnAccountKitLoginSuccess(AuthAccount authHuaweiId)
{
AGConnectAuthCredential credential = HwIdAuthProvider.CredentialWithToken(authHuaweiId.AccessToken);
authServiceManager.SignIn(credential);
}
private void OnAccountKitLoginFailed(HMSException error)
{
// ...
}
public void SignInWithHuaweiAccount()
{
HMSAccountManager.Instance.OnSignInSuccess = OnAccountKitLoginSuccess;
HMSAccountManager.Instance.OnSignInFailed = OnAccountKitLoginFailed;
HMSAccountManager.Instance.SignIn();
}
You need to SignInAnonymously method from AuthServiceManager.
public void SignInAnonymously() => authServiceManager.SignInAnonymously();
First you need to sign in with Facebook Unity SDK and take the access token. And then call AuthService SignIn method and pass the credential parameter with this method.
public void FbLogin()
{
var perms = new List<string>() { "public_profile", "email" };
FB.LogInWithReadPermissions(perms, AuthCallback);
}
private void AuthCallback(ILoginResult result)
{
if (FB.IsLoggedIn)
{
SignInWithFacebook(Facebook.Unity.AccessToken.CurrentAccessToken.TokenString);
}
else
{
Debug.Log($"{Tag} User cancelled login");
}
}
private void SignInWithFacebook(string accessToken)
{
AGConnectAuthCredential credential = FacebookAuthProvider.CredentialWithToken(accessToken);
authServiceManager.SignIn(credential);
}
To sign-in with mobile phone you need to take Country Code, Mobile Number and Password from user. And then pass these parameters with SignIn.
public void SignInWithPhone()
{
AGConnectAuthCredential phoneAuthCredential = PhoneAuthProvider.CredentialWithPassword(PhoneCountryCode.text, PhoneNumber.text, PhonePassword.text);
authServiceManager.SignIn(phoneAuthCredential);
}
For registration, first you need to take verification code. To take this verification code you need to know Country Code and Phone Number. And then call verification code request method.
public void RequestVerifyCodeWithPhoneNumber()
{
VerifyCodeSettings verifyCodeSettings = new VerifyCodeSettings.Builder()
.Action(VerifyCodeSettings.ACTION_REGISTER_LOGIN)
.Locale(Locale.GetDefault())
.SendInterval(60).Build();
PhoneAuthProvider.RequestVerifyCode(registerPhoneCountryCode.text, registerPhoneNumber.text, verifyCodeSettings)
.AddOnSuccessListener(verifyCodeResult =>
{
Debug.Log($"{Tag} Phone Verification Code Sent");
})
.AddOnFailureListener(exception =>
{
Debug.Log($"{Tag} => RequestVerifyCodeWithPhoneNumber {exception}");
});
}
And then you need to call CreateUser method with Verification Code, Country Code, Phone Number and Password.
public void RegisterWithPhoneNumber()
{
PhoneUser phoneUser = new PhoneUser.Builder()
.SetCountryCode(registerPhoneCountryCode.text)
.SetPhoneNumber(registerPhoneNumber.text)
.SetPassword(registerPhonePassword.text)
.SetVerifyCode(registerPhoneVerifyCode.text)
.Build();
authServiceManager.CreateUser(phoneUser);
}
To sign-in with Email adress you need to take e-mail and password from user. And then pass these parameters with SignIn.
public void SignInWithEmail()
{
AGConnectAuthCredential emailAuthCredential = EmailAuthProvider.CredentialWithPassword(Email.text, EmailPassword.text);
authServiceManager.SignIn(emailAuthCredential);
}
For registration, first you need to take verification code. To take this verification code you need to know E-mail adress. And then call verification code request method.
public void RequestVerifyCodeWithEmail()
{
VerifyCodeSettings verifyCodeSettings = new VerifyCodeSettings.Builder()
.Action(VerifyCodeSettings.ACTION_REGISTER_LOGIN)
.Locale(Locale.GetDefault())
.SendInterval(30).Build();
EmailAuthProvider.RequestVerifyCode(registerEmail.text, verifyCodeSettings)
.AddOnSuccessListener(result =>
{
Debug.Log($"{Tag} Email Verification Code Sent");
})
.AddOnFailureListener(error =>
{
Debug.Log($"{Tag} => RequestVerifyCodeWithEmail {error}");
});
}
And then you need to call CreateUser method with Verification Code, E-mail and Password.
public void RegisterWithEmail()
{
EmailUser emailUser = new EmailUser.Builder()
.SetEmail(registerEmail.text)
.SetVerifyCode(registerEmailVerifyCode.text)
.SetPassword(registerEmailPassword.text)
.Build();
authServiceManager.CreateUser(emailUser);
}
Use SignOut method to log out.
authServiceManager.SignOut();
To delete the user from the database of the auth service, use the DeleteUser method.
authServiceManager.DeleteUser();
-
If you received package name error , please check your package name on File->Build Settings -> Player Settings -> Other Settings -> Identification
-
If you received min sdk error , Please set your API level as implied in the Requirements section
-
If you reveived "com.android.support.support-compat" error. Assets -> Huawei -> Editor -> Utils -> HMSGradleWorker.cs
HMS Auth Service Check
MIT