[Huawei Developers] Freemind R:
Certain kind of apps require user registration to provide relevant information or save user's generated content/preferences in a server. If you are building an app with this feature, you may be interested in Huawei Auth Service to quickly develop your Log In screen with support of the most popular Third-party accounts. I've talked about how to set up the Auth Service in a previous post, so now will show you how to add the Sign In with Email to your Huawei Auth Service implementation.

Previous requirements

A developer account

An app project on AGC with Auth Service integrated

Note: Auth service requires a Data Storage Location, be careful when selecting this, because your app will be only available to certain countries per location.

Enabling the service

You must enable the account types to be supported by Auth Service one by one. Enable the Email support by going to AGC > My projects > "Your Project" > Auth Service.

Auth Service configurtion panel

Layout

Add a button and an EditText to your Login screen to get the user's email.

Layout editor

Adding the logic

Add the onClickListener to your login button

mailBtn.setOnClickListener(this)

override fun onClick(v: View?) {
loadingDialog.show()
when (v?.id) {
R.id.hw -> signInWithHWID()

R.id.google_sign_in_button -> signInWithGoogle()

R.id.anon -> signInAnonymously()

R.id.mailBtn -> signInWithMail()
}
}

I suggest you using a regular expression to check if the given email has a proper format. If everything is ok, you can send a verification code to the email address

private fun signInWithMail() {
val input = mail.text.toString()
val regex = Regex("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}\$")
if (!regex.matches(input)) {
Snackbar.make(mailBtn, "Please use a valid email", Snackbar.LENGTH_SHORT).show()
return
}

val settings = VerifyCodeSettings.newBuilder()
.action(ACTION_REGISTER_LOGIN) //ACTION_REGISTER_LOGIN/ACTION_RESET_PASSWORD
.sendInterval(30) // Minimum sending interval, ranging from 30s to 120s.
.build()

val task: Task<VerifyCodeResult> = EmailAuthProvider.requestVerifyCode(
input,
settings
)
task.addOnSuccessListener {
//The verification code application is successful.
Snackbar.make(
mailBtn,
"Verification code sent to your mailbox",
Snackbar.LENGTH_SHORT
).show()
Log.e("EmailAuth", "success")
//Display dialog
val dialog = VerifyDialog(this, input)
dialog.listener = this
dialog.show()
}
.addOnFailureListener {
Log.e("EmailAuth", it.toString())
}

}

In my case, I've chosen to show a dialog, waiting for the verification code.

Dialog layout

Then the dialog will report the given code and password to the Fragment / Activity

class VerifyDialog(val context: Context,val email: String):DialogInterface.OnClickListener{
lateinit var view:View
var listener:VerificationListener?=null
var alertDialog: AlertDialog
init{
val inflater=LayoutInflater.from(context)
view=inflater.inflate(R.layout.dialog_verify,null)
val builder=AlertDialog.Builder(context)
builder.setTitle("Email Verification")
.setView(view)
.setPositiveButton("ok",this)
.setNegativeButton("Cancel",this)
.setCancelable(false)
alertDialog=builder.create()
}

public fun show(){
alertDialog.show()
}

override fun onClick(dialog: DialogInterface?, which: Int) {
when(which){
DialogInterface.BUTTON_POSITIVE ->{
val code=view.inputCode.text.toString()
if(code==""){
Snackbar.make(view,"Please input the code",Snackbar.LENGTH_SHORT).show()
return
}
val pass=view.inputPass.text.toString()
val vPass=view.confirmPass.text.toString()
if(pass.isEmpty()||pass!=vPass){
Snackbar.make(view,"Passwords doesn't match",Snackbar.LENGTH_SHORT).show()
return
}
listener?.onVerification(code,email,pass)
}

DialogInterface.BUTTON_NEGATIVE ->{
dialog?.dismiss()
listener?.onCancel()
}
}
}

public interface VerificationListener{
fun onVerification(code:String,email:String,password:String)
fun onCancel()
}
}

Report the code and password to AGC, so the user can use the password to sign in the next time.

Note: Setting the password is not mandatory.

override fun onVerification(code: String, email: String, password: String) {
val emailUser = EmailUser.Builder()
.setEmail(email)
.setVerifyCode(code)
.setPassword(password) // Optional. If this parameter is set, the current user has created a password and can use the password to sign in.
// If this parameter is not set, the user can only sign in using a verification code.
.build()
AGConnectAuth.getInstance().createUser(emailUser)
.addOnSuccessListener {
// After an account is created, the user is signed in by default.
startNavDrawer(it.user)
}

.addOnFailureListener {
Log.e("AuthSevice","Email Sign In failed $it")
}

}

Conclusion

Now your users can Sign In with their email adress. You can check the full example here: DummyApp

Reference

Official document：https://developer.huawei.com/consum...-connect-Guides/agc-auth-service-introduction