[codelabs challenge example explanation 1] nucleic acid test result authentication query system – authentication login

Time:2022-6-6

The nucleic acid detection results contain a large amount of personal privacy data. How to enable nucleic acid detection personnel to safely store / query massive data in the cloud has become the primary problem of nucleic acid detection data storage.Huawei appgallery connectIt provides two serverless services, authentication service and cloud database service, which can respectively realize basic end cloud collaboration functions such as user authentication login and data writing / query, perfectly solve the query / storage problem of nucleic acid detection data, and ensure the security of data through secure authentication function.

Codelabs challenge – building a zero foundation certification query system is in full swing. Sharing works has the opportunity to get rich gifts such as Huawei freebud Pro wireless headset, Huawei Bracelet 6nfc version, alliance backpack, etc. come and participate > >https://developer.huawei.com/consumer/cn/forum/topic/0202851788421280176?fid=0102822233052010012

Based on the above requirements and business pain points, this paper uses two serverless services, authentication service and cloud database, to build a nucleic acid detection result authentication query system. The system functions and technology selection are as follows:

Development preparation

Open service

For services using appgallery connect (AGC for short), theAGC management consoleOn the AGC console (you can experience all the services provided by AGC in one stop). Before opening the service, you need to register the Huawei developer account. Follow theGuidance documentYes, I won’t repeat it here.

This article mainly introduces the login authentication function of the system, so it only introduces the opening of the corresponding service – authentication service in the application. Before opening the service, you need to create the corresponding project and the application under the project:

1. loginAGC management console, click “my project”, select “add project”, and fill in the project name.

cke_1075286.png

2. click “add application” on the project setting page to jump to, fill in the application information and confirm.

cke_1118047.png

So far, the project and the applications under the project have been created.

3. click “build authentication service” in the menu directory, click “open now” in the upper right corner, and select the data processing location (the data processing location is the desired storage location for processing your application and user data), and generally select “China” in China. Then, select enable the “mobile number” authentication method.

cke_1237881.png

So far, the authentication service has been successfully opened.

Integrated SDK

AGC has opened its anonymous login ability to Huawei accounts, game accounts and other accounts in the form of SDK. Before developing the authentication function, it needs to integrate the authentication service SDK, and download the JSON file provided by AGC to obtain the basic information of the application and the location information of data processing.

1.1 integrated SDK

After you create a project in Android studio, the SDK integration steps in the project are as follows:

1. build Configure Maven bin and AGC plug-in address under gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
  repositories {
    google()
    jcenter()
    //Maven warehouse address
    maven {url 'https://developer.huawei.com/repo/'}
  }
  dependencies {
    classpath "com.android.tools.build:gradle:4.1.0"
    //AGC plug-in address
    classpath 'com.huawei.agconnect:agcp:1.6.5.300'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
  }
}

allprojects {
  repositories {
    google()
    jcenter()
    //Maven warehouse address
    maven {url 'https://developer.huawei.com/repo/'}
  }
}

task clean(type: Delete) {
  delete rootProject.buildDir
}

2. build Configure the compilation dependency and authentication service SDK under the gradle file

plugins {
    id 'com.android.application'
    //Add AGC dependency
    id 'com.huawei.agconnect'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId "com.huawei.covid19detection"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    //AGC core dependency
    implementation 'com.huawei.agconnect:agconnect-core:1.6.5.300'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    //Authentication service SDK
    implementation 'com.huawei.agconnect:agconnect-auth:1.6.5.300'
}

After adding the above SDK or dependency, you need to click “sync now” in the upper right corner to wait for the synchronization to complete.

1.2 add profile

In order to simplify the configuration steps of developers, a configuration file for saving application configuration information is provided to developers. Only add the configuration file to the project directory and integrate the AGC plug-in. The AGC plug-in can automatically load application information into the development environment.

1. open the “project settings” of the project just created, and download the configuration file “agconnect-services.json”.

2. put the downloaded JSON file into the application level directory of the Android studio project, and then download and put the JSON file again after integrating the cloud database.

cke_1441412.png

 

Interface design

To put it simply, using a mobile account to complete the login authentication function with the help of the authentication service requires only the following two points on the interface: having the necessary registration interface (including the input of account number, password and verification code) and having the necessary login interface (this time I used the verification code to log in, so I can enter the account and verification code). The example interface and code are as follows:

Registration interface:

cke_1487763.png

Login interface:

cke_35243.png

Account registration

The nucleic acid detection and query system shall have complete account registration and login functions to ensure the security and reliability of the system. AGC authentication service provides the function of mobile phone account registration. After successful registration, you can quickly log in.

1. after consulting the official documents, it is found that to register a mobile phone account, you need to obtain the verification code first, and AGC also provides an interface to obtain the verification codeAGConnectAuth.requestVerifyCodeFrom the API documents, fill in the country code and mobile phone number, and click “obtain” to apply.

findViewById(R.id.verification_code_obtain).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String phoneNumberStr = editText_num.getText().toString().trim();
        VerifyCodeSettings settings = new VerifyCodeSettings.Builder()
            .action(VerifyCodeSettings.ACTION_REGISTER_LOGIN)
            .sendInterval(30)
            .locale(Locale.CHINA)
            .build();
        Task task = AGConnectAuth.getInstance().requestVerifyCode(countryCodeStr, phoneNumberStr, settings);
        task.addOnSuccessListener(TaskExecutors.uiThread(), new OnSuccessListener() {
            @Override
            public void onSuccess(VerifyCodeResult verifyCodeResult) {
                //Verification code application succeeded
                Log.d(TAG, "onSuccess: message send successfully"+phoneNumberStr);
                Toast.makeText(RegisterActivity.this,"send phone verify code success",Toast.LENGTH_LONG).show();
            }
        }).addOnFailureListener(TaskExecutors.uiThread(), new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                //Account cancellation in case of failure
                //AGConnectAuth.getInstance().deleteUser();
                Log.e(TAG, "onSuccess: message send failed"+phoneNumberStr);
                Toast.makeText(RegisterActivity.this, "requestVerifyCode fail:" + e, Toast.LENGTH_SHORT).show();
            }
        });
    }
});

The toast prompt “send phone verify code success” indicates that the verification code application is successful. The example of the received verification code is as follows:

cke_49288.png

Fill in the verification code in EditText and click “Rigister” to register successfully.

Corresponding toAGConnectAuth.createUserRegister the user interface. After successful registration, please go to the login page to log in.

findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String phoneNumberStr = editText_num.getText().toString().trim();
        String vericode = veri_code.getText().toString().trim();
        String rePsd = repsd_txt.getText().toString().trim();
        PhoneUser phoneUser = new PhoneUser.Builder()
            .setCountryCode(countryCodeStr)
            .setPhoneNumber(phoneNumberStr)
            .setVerifyCode(vericode)
            .setPassword(rePsd)
            .build();
        AGConnectAuth.getInstance().createUser(phoneUser)
            .addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(SignInResult signInResult) {
                //After the account is created successfully, it is logged in by default and enters the main page
                Intent intent = new Intent();
                intent.setClass(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                Toast.makeText(RegisterActivity.this, "register fail:" + e, Toast.LENGTH_SHORT).show();
            }
        });
    }
});

Login authentication

After successful registration, there are two ways to log in the application using AGC authentication service: verification code login and password login. Only verification code login is implemented here. Please refer toGuidance document

1. the interface involved in the application verification code is consistent with the account registration. Refer to the application.

2. use the obtained verification code to generate mobile account login credentials. Since we have not set the enter password box, you can fill in null for the password parameter here.

AGConnectAuthCredential credential = PhoneAuthProvider.credentialWithVerifyCode(countryCodeStr, phoneNumberStr, null, userVeri);

3. log in to the application using the generated account credentials. SeeAGConnectAuth.signIn

findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String phoneNumberStr = loginText_num.getText().toString().trim();
        String vericode = loginVeri_code.getText().toString().trim();
        AGConnectAuthCredential credential = PhoneAuthProvider.credentialWithVerifyCode(countryCodeStr,phoneNumberStr,"",vericode);
        AGConnectAuth.getInstance().signIn(credential)
            .addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(SignInResult signInResult) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, DatabaseActivity.class);
                startActivity(intent);
                Log.i(TAG, "onSuccess: login success");
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {}
        });
    }
});

After successful login, you can jump to the main application interface. The main interface displays the nucleic acid test result data. The data import and display will be described in detail in the next guide.

summary

The AGC authentication service is used to register and log in the account by applying for a verification code. The main workload of the whole development process is in the early service opening and SDK integration. The amount of core function code development is small, and the SDK size is controlled within MB (seeSecrets of Huawei AGC SDK size)。

In the next section, we will introduce how to use AGC’s cloud database to import / record nucleic acid test result data and query and display them.

Reference documents:

How to use a mobile account to register an application:https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-auth-android-phone-0000001053333941#section204291139102115

API reference for certification service registration and login:

https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-References/agconnectauth-0000001054482530

Required reading for development preparation and integration SDK:

https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-get-started-android-0000001058210705

Attach another example explanation:[codelabs challenge example explanation 2] nucleic acid test result authentication query systemData import and query