当前位置:网站首页>[AGC] build service 3 - authentication service example
[AGC] build service 3 - authentication service example
2022-07-02 11:01:00 【Huawei Developer Forum】
Preface : Last time I brought you AGC In the field of App Linking Learning from . This time we will continue to deepen our study AGC Knowledge about . Before the article begins , Give the reader a brief introduction AGC, In case the first time readers don't understand . So-called AGC Namely AppGallery Connect For short , Create ideas for users' applications 、 Development 、 distribution 、 operating 、 Provide one-stop service in all links of operation , Put it in a more general way , It is to provide comprehensive services for users to operate applications on the shelf in Huawei application market .
All of the AGC Relevant knowledge is divided into two categories , One major category is related to on the shelf , This can refer to the link :https://developer.huawei.com/consumer/cn/doc/distribution/app/agc-help-overview-0000001100246618. It provides application testing 、 Application of release 、 Application management 、 Application maintenance and other application market related content . Another kind of help for developers Kit, This can refer to the link :https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-introduction-0000001057492641. It provides things like cloud storage 、A/B test 、 Quality analysis 、 Huawei cloud, etc Kit, Provide additional help to developers , Assist developers to better complete the application on the shelf 、 Improve application quality 、 Increase user stickiness, etc .
because AGC There's too much of it , So every time we study, we try to talk about one of them , To be more specific . This time, let's learn the function of the authentication service of the build service . This function can quickly build a safe and reliable user authentication system for applications , You only need to access the relevant capabilities of the authentication service in the application , You don't need to care about the facilities and Implementation on the cloud side . Certification services provide SDK And back end services , Built in support for multiple authentication methods , Provides a powerful management console , It can easily complete the development and management of user authentication . Certification services SDK Support multiple platforms and languages , No matter what type of terminal the user uses , Can get a unified login experience and user identity . Certification services act as Serverless One kind of service can be compared with others Serverless Service auto adaptation , Users can be protected by simple rule definitions Serverless Data security in services .
The main functions of the authentication service :1、 Mobile phone accounts , Authenticate users by their mobile phone numbers , Users can use their mobile phone number and password or mobile phone number and verification code to log in to the application . The authentication service provides registration based on mobile phone number 、 Sign in 、 Password change 、 Password reset 、 Verify SMS push capabilities and interfaces .2、 Email account number , Authenticate users by email address , Users can use email address and password or email address and verification code to log in to the application . The authentication service provides email address based registration 、 Sign in 、 Password change 、 Password reset 、 Verify e-mail push capabilities and interfaces .3、 Third party accounts , Identity authentication for users through third-party authentication services . Users can use a third-party account to log in to the application .4、 Own account number , If you have built your own authentication system , You can connect your own account to make the authentication system you have built work with the authentication service , To achieve the following purposes . Let the authentication service provide authentication methods that your own authentication system does not have ; Allow users in your own authentication system to access others in a secure way Serverless service ( For example, cloud database 、 Cloud storage, etc ).5、 Anonymous account supports the visitor access mode of the application . The authentication service can assign a user ID to your visitors , Enable you to identify different tourists and provide them with differentiated services , And give your visitors safe access to other Serverless service . Tourists can be transformed into official users by associating with other authentication methods , And keep its original user ID unchanged , To keep its business consistent .
Implementation process of authentication service .1、 Enable authentication mode , Enable the authentication methods you want to support in the authentication services console , And provide necessary configuration information according to the interface guidance . For third-party account authentication , The application identification and secret key applied in the third-party authentication system shall be provided .2、 Implement the login interface process on the application client , Implement the interface flow required for user login in the application client code :1) For mobile phone account and email account authentication , Please input the account information 、 Password or verification code information interface process , And realize registration 、 Sign in 、 Password change 、 Password reset 、 Verification code push and other processes .2) For third-party account authentication , The login interface process shall be implemented according to the requirements of the third-party authentication system .3) For anonymous account authentication , You need to help or guide visitors to complete anonymous login .4) For self owned account authentication , You can keep your original login experience , And add the authentication service requirements in the process Token Generation and transmission of .3、 End side integrated authentication service SDK, Integrate the authentication service in the end-to-end code of the application SDK, And pass the certification service SDK Submit authentication credentials 、 Receive the certification results .
Then follow the steps of the author to complete a simple authentication service . Because this is just for demonstration , So part of the content has been simplified . Authentication services can also support other platforms , Readers can explore for themselves .
1、 Integrate SDK
1) stay AGC Select the application that needs to enable authentication service in my project of the website , Click on the building -> Certification services , Enter the authentication service page , Activate service now , Click on “ Phone number ” The line where the authentication method is located " Enable ". My side is already open .
2) Sign in AGC Website , Click my project , Under the application column of project settings , Download profile "agconnect-services.json". Copy the configuration file to the application level root directory .
3) add to AGC plug-in unit , stay Android Studio Project level build.gradle Add... To the file maven Warehouse address and HUAWEI agc plug-in unit .
4) open Android Studio Application level build.gradle file , Add add agcp Plug in configuration and agc-auth Compile dependencies .
5) Click... On the interface "Sync Now" Link sync completed configuration .
2、 Interface design
1) stay Android Studio Create a layout page in the project , Refer to the following figure UI Design , Have the ability to input the country code 、 Input box for mobile phone number and verification code, and button for obtaining verification code and login .
Xml Layout code :
<?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=".Activity.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=" Please enter your mobile 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=" Please enter the 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=" 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>
3、 Certification services sdk Access code implementation
1) stay EditText Enter the country code and mobile phone number respectively , Click the verification code button to call requestVerifyCode Method to get the verification code ,SDK Request to send the authentication code to the authentication service server , Input the received verification code into the verification code EditText in .
// Send verification code method
public void sendVerifyCode() {
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, " Verification code has been sent .",
Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(TaskExecutors.uiThread(), new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(LoginActivity.this, " Sending verification code failed ." + e,
Toast.LENGTH_SHORT).show();
Log.e(TAG, "requestVerifyCode fail:" + e);
}
});
} else {
Toast.makeText(LoginActivity.this, " Please enter the phone number and country code .",
Toast.LENGTH_SHORT).show();
}
}
2) Click the mobile number login button , Use the country code 、 The mobile phone number and the received verification code construct the credentials required for the associated account .
call AGConnectAuth.signIn Use generated credential The certificate is used to authenticate the mobile phone number , Print mobile phone users uid To... Below the button TextView.
// Phone number login method
public void phoneLogin(){
String countryCode = et_countryCode.getText().toString().trim();
String phoneNumber = et_phoneNumber.getText().toString().trim();
String verifyCode = et_verifyCode.getText().toString().trim();
// Login logic
AGConnectAuthCredential credential = PhoneAuthProvider.credentialWithVerifyCode(
countryCode,
phoneNumber,
null, // password, can be null
verifyCode);
AGConnectAuth.getInstance().signIn(credential)
.addOnSuccessListener(new OnSuccessListener<SignInResult>() {
@Override
public void onSuccess(SignInResult signInResult) {
// Get login information
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, " Login failed ." +e.getMessage(),
Toast.LENGTH_SHORT).show();
Log.e(TAG, "Login error, error:" + e.getMessage());
}
});
}
3) Judge string Not empty method
// Judge string Not empty method
public Boolean notEmptyString(String string){
if (string == null || "".equals(string)){
return false;
}else {
return true;
}
}
4、 Package test
1) function Android Studio Project generation APK package , Install... In the test phone APK package .
2) Enter the country code and mobile number in the interface , Click the verification code button to obtain the verification code .
3) After obtaining the verification code, enter the verification code in the verification code column , Click the login button to log in with your mobile number . I will jump to the main interface after logging in .
above , This time's content sharing , thank you !
For more details , Please see the :
Huawei official website :
https://developer.huawei.com/consumer/cn/forum/topic/0203912789884890322?fid=0101271690375130218?ha_source=zzh
边栏推荐
- 1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
- HDU1234 开门人和关门人(水题)
- P1055 [NOIP2008 普及组] ISBN 号码
- Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)
- Hdu1228 a + B (map mapping)
- 14. Code implementation of semaphore
- [TS] 1368 seconds understand typescript generic tool types!
- 二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)
- 主键策略问题
- 首份中国企业敏捷实践白皮书发布| 附完整下载
猜你喜欢
【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
华为应用市场应用统计数据问题大揭秘
二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)
Jsp webshell Free from killing - The Foundation of JSP
HDU1236 排名(结构体排序)
(5) Gear control setting of APA scene construction
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
【深入浅出玩转FPGA学习5-----复位设计】
MySQL数据库远程访问权限设置
Special topic of binary tree -- Logu p1229 traversal problem (the number of traversals in the middle order is calculated when the pre and post order traversals of the multiplication principle are know
随机推荐
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
【AGC】构建服务3-认证服务示例
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
计算序列之和
Easyexcel, a concise, fast and memory saving excel processing tool
UVM - usage of common TLM port
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
[AI application] Hikvision ivms-4200 software installation
Hdu1228 a + B (map mapping)
一招快速实现自定义快应用titlebar
Set the playback speed during the playback of UOB equipment
PCL Eigen介绍及简单使用
QT学习日记8——资源文件添加
Overview of integrated learning
JSP webshell免杀——JSP的基础
P1055 [noip2008 popularization group] ISBN number
Filtering of PCL
二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
华为快应用中如何实现同时传递事件对象和自定义参数
Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)