处理所有Firebase登录并在Kotlin中注册错误

时间:2018-06-30 09:02:17

标签: android firebase kotlin firebase-authentication

我正在尝试在Android上使用Firebase和Kotlin登录并注册用户。如果一切都很好,我的用户可以正常登录并正常注册,但是例如当我关闭Internet连接时,它将不起作用。我想处理任何错误,以便根据错误向用户显示一些特定的消息。

fun login(email:String, password:String){
        val user = email.split("@")[0]
        val mProgressDialog2 = indeterminateProgressDialog(" $user Connecting to your account...")
        mProgressDialog2.setCancelable(false)
        mAuth!!.signInWithEmailAndPassword(email, password)
                .addOnFailureListener(object:OnFailureListener {
                  override  fun onFailure(@NonNull e:Exception) {
                      //this is the when i tried but does not work
                      //when there is an error line 91 is instead runned
                      when(e.toString()){
                          "NO_NETWORK" -> {
                              Log.d("TAG", "No internet")
                              mProgressDialog2.dismiss()
                          }

                      }
                    }})
                .addOnCompleteListener { task: Task<AuthResult> ->

                    if (task.isSuccessful){
                        val homeIntent = Intent(this, HomeActivity::class.java)
                        homeIntent.putExtra("firstname", user)
                        startActivity(homeIntent)
                        finish()
                        mProgressDialog2.dismiss()
                        Toast.makeText(this, "Welcome $user you are logged in !"
                                , Toast.LENGTH_LONG).show()
                    }else{
                        mProgressDialog2.dismiss()
                            Toast.makeText(this, "$user your login failed, please try again :( "
                                    , Toast.LENGTH_LONG).show()
                    }
                }
    }

0 个答案:

没有答案