Overview

Auth Service provides cloud services and an SDK to help you quickly build a secure and reliable user authentication system for your app. It supports multiple authentication modes and is closely integrated with other serverless services for you to effectively protect user data by defining simple rules.
By integrating the Auth Service SDK, you can connect to multiple third-party user authentication systems, such as HUAWEI ID, WeChat, Facebook, and Twitter, and report authentication credentials to the AppGallery Connect Auth Service server. When a user signs in to your app again, it can obtain user information and other data protected by security rules in other serverless services from Auth Service.
Auth Service can greatly reduce your investment and costs in building an authentication system and its O&M.

What You Will Create

In this codelab, you will build an Android app that integrates Auth Service to authenticate sign-ins. Your app will allow users to:

What You Will Learn

Development Environment and Skill Requirements

Device Requirements

A device running Android 4.2 or a later version

To integrate Auth Service of AppGallery Connect, you must complete the following preparations:

For details, please refer to Integration Preparations.
  1. Sign in to AppGallery Connect, click My projects, click your project card, and select an app for which you want to enable Auth Service from the drop-down list on the top.
  2. Go to Build > Auth Service and click Enable now.
  3. Click Enable for each authentication mode you want to enable. In this codelab, click Enable for HUAWEI ID and Anonymous account.
    Set Client ID and Client Secret, which can be found in project settings.

If you are using Android Studio, you can integrate the SDK into your Android Studio project by using the Maven repository before development.

  1. In AppGallery Connect, click My projects, click your project card, and select an app for which you want to enable Auth Service from the drop-down list on the top.
  2. Go to Project settings > General information. In the App information area, click agconnect-services.json to download it.
  3. Copy the agconnect-services.json file to your app's module directory.
  4. Open the app-level build.gradle file and add the following code to integrate the Auth Service SDK:
    // Configure the following address: apply plugin: 'com.huawei.agconnect' dependencies { // Configure the following addresses: implementation 'com.huawei.agconnect:agconnect-auth:1.6.2.300' implementation "com.huawei.agconnect:agconnect-auth-huawei:1.6.2.300" }
  5. Click Sync Now to synchronize the configuration.

You can create an app page in your Android Studio project and design its UI according to the following figure, with simple entries for users to sign in anonymously and using a HUAWEI ID.

The layout code is as follows:

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the Codelab" android:textSize="24dp" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.2" /> <Button android:id="@+id/anonymous_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Anonymous Login" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.35" /> <TextView android:id="@+id/anonymous_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dp" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5" /> <Button android:id="@+id/huaweiid_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="HuaweiID Login" android:textAllCaps="false" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.65" /> <TextView android:id="@+id/result_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dp" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.8" /> </androidx.constraintlayout.widget.ConstraintLayout>
  1. When ANONYMOUS LOGIN is tapped, generate the user's UID if the sign-in is successful.
    Button anonymousBtn = findViewById(R.id.anonymous_button); anonymousBtn.setOnClickListener(view -> AGConnectAuth.getInstance().signInAnonymously() .addOnSuccessListener(signInResult -> { AGConnectUser user = signInResult.getUser(); String uid = user.getUid(); anonymousResult.setText(uid); Log.i(TAG, "UidValue: " + uid); }).addOnFailureListener(e -> { anonymousResult.setText("Anonymous SignIn failed"); Log.e(TAG, "UidValue: " + e.getMessage()); }));
  2. When HuaweiID Login is tapped, bind the HUAWEI ID to the signed-in anonymous account. After the binding is successful, generate the user's UID and check whether the UID is the same as that of the anonymous account.
    Button huaweiidButton = findViewById(R.id.huaweiid_button); huaweiidButton.setOnClickListener(view -> AGConnectAuth.getInstance().getCurrentUser().link(MainActivity.this, AGConnectAuthCredential.HMS_Provider) .addOnSuccessListener(signInResult -> { String hwUid = signInResult.getUser().getUid(); result.setText(hwUid); Log.i(TAG, "UidValue: " + hwUid); }).addOnFailureListener(e -> { result.setText("HWID SignIn failed"); Log.e(TAG, "UidValue: " + e.getMessage()); }));
  1. Run your project in Android Studio to generate an APK. Before app packaging, sign your app, and set the SHA-256 signature in project settings of AppGallery Connect.
  2. Install the APK on the test mobile phone.
  3. Tap ANONYMOUS LOGIN and check whether the UID of the current user is displayed.
  4. Tap HuaweiID Login and check whether the UID after the HUAWEI ID is bound is displayed. Check whether the UID is the same as that in anonymous sign-in.

Well done. You have successfully built an app that integrates Auth Service of AppGallery Connect and learned how to build a user authentication system using the service.

API Reference
Source code

Code copied