0
点赞
收藏
分享

微信扫一扫

【AGC】如何使用认证服务与云数据库处理用户信息

使用场景

华为 AGC认证服务可以为应用快速构建安全可靠的用户认证系统,可以实现多种方式关联认证登录。而如何处理这些多种登录方式的用户信息,例如在应用中发布一个活动,哪些用户参加了哪一个活动,这些信息都可以记录下来。这样AGC的云数据库服务就派上了用场,无需搭建服务器就可以使用的端云协同的数据库产品。结合认证服务与云数据库服务,就可以简单轻松地完成用户信息录入和修改。下面就将带来一个简单的示例,快速集成认证服务与云数据库实现用户数据处理。

集成准备

在开发功能前,需要开通服务、集成SDK等准备。

1、登录AppGallery Connect网站,点击“我的项目”。在导航选择“构建 > 认证”。如果是首次使用认证服务,请点击“立即开通”开通服务。

【AGC】如何使用认证服务与云数据库处理用户信息_android

2、本次示例以手机号登录为例,所以点击启用手机号码认证方式。

【AGC】如何使用认证服务与云数据库处理用户信息_android_02

3、登录AppGallery Connect网站,点击“我的项目”。在导航选择“构建 > 云数据库”。如果是首次使用云数据库,请点击“立即开通”开通服务。

【AGC】如何使用认证服务与云数据库处理用户信息_云数据库_03

4、​​新增并导出对象类型​​,本次创建的对象类型示例如图。

【AGC】如何使用认证服务与云数据库处理用户信息_云数据库_04

5、点击“存储区”,新增存储区“UsecaseDemo”。

【AGC】如何使用认证服务与云数据库处理用户信息_云数据库_05

6、集成认证服务SDK。

7、集成云数据库SDK并导入对象类型。

布局设计

1、参考如下设置布局,具备输入手机号码、获取验证码、输入验证码登录、登出的功能。

<?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=".LoginActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to AGC usecase"
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"

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.35">
<EditText
android:id="@+id/et_countryCode"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_marginRight="10dp"
android:hint="country code"
android:layout_marginLeft="20dp"
android:inputType="number"
<EditText
android:id="@+id/et_phoneNumber"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:hint="phone number"
android:inputType="number"/>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.45">

<EditText
android:id="@+id/et_verifyCode"
android:layout_width="170dp"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:hint="verification code"
android:inputType="number"
<Button
android:id="@+id/bt_obtain"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="20dp"
android:textSize="15sp"
android:text="Obtaining Verification Code"
</LinearLayout>

<LinearLayout
android:layout_width="200dp"
android:layout_height="45dp"
android:gravity="center_horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.7">

<Button
android:id="@+id/bt_phoneLogin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="Mobile number login"
android:textSize="18sp"
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

【AGC】如何使用认证服务与云数据库处理用户信息_认证服务_06

2、效果展示:

【AGC】如何使用认证服务与云数据库处理用户信息_android_07

功能实现

用户打开应用后将会提示登录,使用的是认证服务的手机登录功能,输入手机号,获取验证码登录成功后跳转至主页。

1、在应用的登录界面,初始化​​AGConnectAuth​​实例,获取AGC的用户信息,检查是否有已经登录的用户。如果有则可以直接进入用户界面,否则显示登录界面。

AGConnectUser user = AGConnectAuth.getInstance().getCurrentUser(); 
if (user != null){
Toast.makeText(this,"已检测到用户,等待自动登录",Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
String uid = user.getUid();
String phoneNumber = user.getPhone();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("uid", uid);
intent.putExtra("phoneNumber", phoneNumber);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
},1000*2);
}

【AGC】如何使用认证服务与云数据库处理用户信息_android_08

2、申请手机注册的验证码,在使用手机注册之前,需要先验证您的手机,确保该手机归您所有。通过​​VerifyCodeSettings.Builder​​生成settings ,然后调用​​AGConnectAuth.requestVerifyCode​​申请验证码。

VerifyCodeSettings settings = VerifyCodeSettings.newBuilder() 
.action(VerifyCodeSettings.ACTION_REGISTER_LOGIN)
.sendInterval(30)
.locale(Locale.SIMPLIFIED_CHINESE)
.build();
String countryCode = et_countryCode.getText().toString().trim();
String phoneNumber = et_phoneNumber.getText().toString().trim();
if (notEmptyString(countryCode) && notEmptyString(phoneNumber)) {
Task<VerifyCodeResult> task = PhoneAuthProvider.requestVerifyCode(countryCode, phoneNumber, settings);
task.addOnSuccessListener(TaskExecutors.uiThread(), new OnSuccessListener<VerifyCodeResult>() {
@Override
public void onSuccess(VerifyCodeResult verifyCodeResult) {
Toast.makeText(LoginActivity.this, "验证码已发送。",
Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(TaskExecutors.uiThread(), new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(LoginActivity.this, "发送验证码失败。" + e,
Toast.LENGTH_SHORT).show();
Log.e(TAG, "requestVerifyCode fail:" + e);
}
});
} else {
Toast.makeText(LoginActivity.this, "请输入电话号码和国家码。",
Toast.LENGTH_SHORT).show();
}

【AGC】如何使用认证服务与云数据库处理用户信息_认证服务_09

3、调用​​ PhoneAuthProvider.credentialWithVerifyCode​​采用验证码方式创建手机帐号凭证。

AGConnectAuthCredential credential = PhoneAuthProvider.credentialWithVerifyCode( 
countryCode,
phoneNumber,
null, // password, can be null

【AGC】如何使用认证服务与云数据库处理用户信息_认证服务_10

4、获取credential成功后,调用​​AGConnectAuth.signIn​​实现登录。

AGConnectAuth.getInstance().signIn(credential) 
.addOnSuccessListener(new OnSuccessListener<SignInResult>() {
@Override
public void onSuccess(SignInResult signInResult) {
//获取登录信息
String uid = signInResult.getUser().getUid();
String phoneNumber = signInResult.getUser().getPhone();
redirect(uid,phoneNumber);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(LoginActivity.this, "登录失败。" +e.getMessage(),
Toast.LENGTH_SHORT).show();
Log.e(TAG, "Login error, error:"

【AGC】如何使用认证服务与云数据库处理用户信息_认证服务_11

5、通过​​initialize()​​方法初始化AGConnectCloudDB。

public static void

【AGC】如何使用认证服务与云数据库处理用户信息_云数据库_12

6、使用​​createObjectType()​​创建对象类型。

public void createObjectType() { 
try {
mCloudDB.createObjectType(ObjectTypeInfoHelper.getObjectTypeInfo());
Log.i(TAG,"createObjectType: success" );
} catch (AGConnectCloudDBException e) {
Log.e(TAG, "createObjectType: "

【AGC】如何使用认证服务与云数据库处理用户信息_android_13

7、创建Cloud DB zone配置对象,并打开该Cloud DB zone。

mConfig = new CloudDBZoneConfig("UsecaseDemo", 
CloudDBZoneConfig.CloudDBZoneSyncProperty.CLOUDDBZONE_CLOUD_CACHE,
CloudDBZoneConfig.CloudDBZoneAccessProperty.CLOUDDBZONE_PUBLIC);
mConfig.setPersistenceEnabled(true);
Task<CloudDBZone> openDBZoneTask = mCloudDB.openCloudDBZone2(mConfig, true);
openDBZoneTask.addOnSuccessListener(cloudDBZone -> {
Log.i(TAG, "Open cloudDBZone success");
mCloudDBZone = cloudDBZone;
addSubscription();
}).addOnFailureListener(e -> {
Log.e(TAG, "Open cloudDBZone failed for "

【AGC】如何使用认证服务与云数据库处理用户信息_认证服务_14

8、通过​​executeUpsert()​​将用户信息插入到云数据库。

public void upsertUserInfo(UserInfo userInfo) { 
if (mCloudDBZone == null) {
Log.w(TAG, "CloudDBZone is null, try re-open it");
return;
}
Task<Integer> upsertTask = mCloudDBZone.executeUpsert(userInfo);
upsertTask.addOnSuccessListener(cloudDBZoneResult -> {
ArrayList<UserInfo> userInfos = new ArrayList<>();
userInfos.add(userInfo);
Log.i(TAG, "Upsert " + cloudDBZoneResult + " records");
}).addOnFailureListener(e -> {
Log.e(TAG, "upsert Error: "

【AGC】如何使用认证服务与云数据库处理用户信息_android_15

9、查询所有用户的信息。

public void queryUserInfo(CloudDBZoneQuery<UserInfo> query) { 
if (mCloudDBZone == null) {
Log.w(TAG, "CloudDBZone is null, try re-open it");
return;
}
Task<CloudDBZoneSnapshot<UserInfo>> queryTask = mCloudDBZone.executeQuery(query,
CloudDBZoneQuery.CloudDBZoneQueryPolicy.POLICY_QUERY_FROM_CLOUD_ONLY);
queryTask.addOnSuccessListener(new OnSuccessListener<CloudDBZoneSnapshot<UserInfo>>() {
@Override
public void onSuccess(CloudDBZoneSnapshot<UserInfo> snapshot) {
Log.w(TAG, "Query list from cloud userInfos success");
processQueryResult(snapshot);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG, "Query list from cloud failed Error: "

【AGC】如何使用认证服务与云数据库处理用户信息_android_16

功能测试

将项目打包后测试,使用手机号认证登录,选择某一预置的活动参加,在AGC控制台云数据库中就可以查看登录用户的信息。

【AGC】如何使用认证服务与云数据库处理用户信息_android_17

这样就可以快速实现认证服务登录用户信息的增删改查。

参考文档

云数据库:​​https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-clouddb-introduction-0000001054212760​​

认证服务:​​https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-auth-introduction-0000001053732605​​


欲了解更多更全技术文章,欢迎访问​​https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh​​

举报

相关推荐

0 条评论