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.
In this codelab, you will build an Android app that integrates Auth Service to authenticate sign-ins. Your app will allow users to:
A device running Android 4.2 or a later version
To integrate Auth Service of AppGallery Connect, you must complete the following preparations:
If you are using Android Studio, you can integrate the SDK into your Android Studio project by using the Maven repository before development.
// 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"
}
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>
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());
}));
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());
}));
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.