能够在Firebase身份验证中对用户进行身份验证,但无法在Firebase数据库中存储数据

时间:2018-01-11 15:33:04

标签: android firebase firebase-realtime-database kotlin rx-java2

我可以通过Firebase身份验证对用户进行身份验证,但无法在Firebase数据库中存储数据。我没有在logcat中收到任何错误。我还没有更改Firebase数据库中的任何规则。我的规则设置为默认值。这是我的代码:

fun authenticateUser(){
    Observable.create(ObservableOnSubscribe<Task<AuthResult>>{
        e: ObservableEmitter<Task<AuthResult>> -> run {
        try {
            firebaseAuth.createUserWithEmailAndPassword(email!!, password!!)
                    .addOnCompleteListener(this, object : OnCompleteListener<AuthResult> {
                        override fun onComplete(p0: Task<AuthResult>) {
                            e.onNext(p0)
                            e.onComplete()
                        }
                    })
        }
        catch (ex : Exception){
            dialogs?.dismiss()
            displayMessage("network problem..")
            e.onError(ex)
        }
    }
    }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Task<AuthResult>>{
                override fun onError(e: Throwable) {
                    e.printStackTrace()
                }

                override fun onNext(t: Task<AuthResult>) {
                    if (t.isSuccessful) {
                        storeData()
                    }
                    else if (!t.isSuccessful){
                        dialogs?.dismiss()
                        displayMessage("some issues has came..")
                    }
                }
                override fun onComplete() {
                }

                override fun onSubscribe(d: Disposable) {
                    compositeDisposable?.add(d)
                }
            })
}

override fun storeData() {
    var currentUser = firebaseAuth.currentUser
    val databaseReference = firebaseDatabase.reference.child("Users_Information").child(currentUser?.uid)
    val data = HashMap<String,String>()
    data.put("name",nameString!!)
    data.put("email_address",emailString!!)
    data.put("image","def")
    data.put("thumb_image","def")

    databaseReference.setValue(data).addOnCompleteListener {
        task -> if (task.isSuccessful){
                dialogs?.dismiss()
                displayMessage("user is created")
                goToNextActivity()
        }
        else if (!task.isSuccessful){
        dialogs?.dismiss()
        displayMessage("Authentication failed, try again later")
     }
        else if (task.isComplete){
        displayMessage("data not stored")
     }
    }
}

override fun goToNextActivity() {
    var intents = Intent(this,MainActivity::class.java)
    intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
     intents.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
    startActivity(intents)
    finish()
}

Uid正在生成但数据未存储在Firebase数据库中。我调试代码但没有发现任何问题。问题出在哪里,请帮忙......

1 个答案:

答案 0 :(得分:0)

尝试将Firebaseuser发送到storeData()并尝试在storeData()中记录Firebaseuser

override fun onNext(t: Task<AuthResult>) {
                if (t.isSuccessful) {
                    Log.d("FB user", t.getResult().getUser())
                    storeData(t.getResult().getUser())
                }
                else if (!t.isSuccessful){
                    dialogs?.dismiss()
                    displayMessage("some issues has came..")
                }
            }

....
..

  override fun storeData(curentUser: Firebaseuser) {
      Log.d("FB user", currentUser?.uid)
      val databaseReference = firebaseDatabase.reference.child("Users_Information").child(currentUser?.uid)
...
..

 }