How to use Auth Service and CloudDB in app?

Melike Karayılan
Huawei Developers
Published in
4 min readMay 13, 2020

Hi everyone.

In this article, I will explain how to use Auth Service and CloudDB in a mobile app.

The properties of the applicaton that will be developed:

  • Being able to sign in with more than one way for the users.
  • Being able to create their own todolists after they signed in and list them instantly.

Developing Process:

  • Deciding which services will meet the needs of the application.
  • I used Auth Service instead of Account Kit to have more than one sign in method.
  • I used CloudDB service to make users able to save data through the application.
  • I’ve created my project in Huawei App Gallery before all the integrations.
  • After that, I received the permission with the AppId since it’s a CloudDB beta version.
  • I matched the package name of the app that I’ve created in the android studio with my app in AppGallery then I’ve started the service integrations.

Integration of Auth Service:

  • First, I’ve added the true version of AuthService into my build.gradle file.
  • After that, I’ve added the methods which will manage sign in and sign out processes by creating my HuaweiAccountHelper class.
  • I used huaweiIdAuthManager for initialize to HuaweiIdAuthService object. This code block as follows:
HuaweiAccountHelper(Context context){
HuaweiIdAuthParams huaweiIdAuthParams = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setId()
.setEmail()
.setAccessToken()
.createParams();

huaweiIdAuthService = HuaweiIdAuthManager.getService(context, huaweiIdAuthParams);

}
  • AccessToken is received whenever the sign in is successful and this token is used to create AGConnectAuthCredential object. This code block as follows:
AGConnectAuthCredential credential = HwIdAuthProvider.credentialWithToken(huaweiAccount.getAccessToken());
if(null != huaweiSignInCallback){
huaweiSignInCallback.onSignInSuccess(credential);
}
} else {
Log.i(TAG, "signIn failed: " + ((ApiException) authHuaweiIdTask.getException()).getStatusCode());
huaweiSignInCallback.onSignInFail();
}
private void authWithHuawei(AGConnectAuthCredential credential) {
AGConnectAuth.getInstance().signIn(credential)
.addOnSuccessListener(signInResult -> redirectToToDoList())
.addOnFailureListener(e -> Toast.makeText(HuaweiIdActivity.this, "Huawei Auth failed! e->" + e.getMessage(), Toast.LENGTH_SHORT).show());
}

Integration of CloudDB:

Application uses Clouddb for users to create their todo list. You need permission with AppId, if you want to use cloudDb. I’ve created a clouddb zone at app gallery console. (CloudDB Zone: A Cloud DB zone is an independent data storage zone, and multiple Cloud DB zones are independent of each other.) As follows:

  • I’ve exported classes which are objecthelper and todolistmodel in order to add them to the application.(Object Type: An object type is used to define a set of stored object data. Different object types correspond to different data structures.) As follows:
  • Sample ObjectType (Object: A CloudDBZoneObject is a basic operation unit of Cloud DB. Each object is a complete data record.)
  • I have created CloudDB model for all database processes. I used createObjectType, openCloudDbZone, closeCloudDbZone, executeUpsert an onSnapShot(Add mSnapshotListener to monitor data changes from storage) methods in CloudDbModel class. Initialize AGConnectCloudDB, 2. Obtain the AGConnectCloudDB instance and create object types. 3. Create a Cloud DB zone configuration file, open the Cloud DB zone, and set the name, data synchronization property, and access property of the Cloud DB zone.) This code block as follows:
public void createObjectType() {
try {
mCloudDB.createObjectType(ObjectTypeInfoHelper.getObjectTypeInfo());
} catch (AGConnectCloudDBException e) {
Log.w(TAG, "createObjectType: " + e.getMessage());
}
}

public void openCloudDBZone() {
mConfig = new CloudDBZoneConfig("CloudDBModel",
CloudDBZoneConfig.CloudDBZoneSyncProperty.CLOUDDBZONE_CLOUD_CACHE,
CloudDBZoneConfig.CloudDBZoneAccessProperty.CLOUDDBZONE_PUBLIC);
mConfig.setPersistenceEnabled(true);
try {
mCloudDBZone = mCloudDB.openCloudDBZone(mConfig, true);
} catch (AGConnectCloudDBException e) {
Log.w(TAG, "openCloudDBZone: " + e.getMessage());
}
}
  • Createobjecttype and openclouddbzone methods should be called before creating any record. Therefore, I called both two methods in the classes of todolistactivity and addnewitemactivity. Also, I used addsubscription method in todolistactivity to be able to see\mirror the instant changes in the records. This code block as follows:
public void addSubscription() {
if (mCloudDBZone == null) {
Log.w(TAG, "CloudDBZone is null, try re-open it");
return;
}

try {
CloudDBZoneQuery<TodoListModel> snapshotQuery = CloudDBZoneQuery.where(TodoListModel.class);
mRegister = mCloudDBZone.subscribeSnapshot(snapshotQuery,
CloudDBZoneQuery.CloudDBZoneQueryPolicy.POLICY_QUERY_FROM_CLOUD_ONLY, mSnapshotListener);
} catch (AGConnectCloudDBException e) {
Log.w(TAG, "subscribeSnapshot: " + e.getMessage());
}
}

References

--

--