简体   繁体   English

无法在 kotlin 中获取 firebase 当前用户

[英]Unable to get firebase current user in kotlin

I am integrating FirebaseAuth into an Android app that I am building.我正在将 FirebaseAuth 集成到我正在构建的 Android 应用程序中。 I have successfully integrated firebase into the app by adding the SDK, downloading and adding the google-services.json file.通过添加 SDK、下载并添加 google-services.json 文件,我已成功将 firebase 集成到应用程序中。

The problem occurs when I try to Sign Up a new user.当我尝试注册新用户时出现问题。 The user gets added to firebase console but I cannot retrieve the signed in user to update the name.用户被添加到 firebase 控制台,但我无法检索登录用户来更新名称。 This is the code from the fragment I am using it in.这是我正在使用它的片段中的代码。

requireActivity().let {
    val user = authenticationService.signUpWithEmailAndPassword(email, password)

    user?.let {
            authenticationService.updateUser(it, name)
            navigationService.openHomeScreen()//open next creen
    }
}

the signup function in the authenticationService在 authenticationService 中注册 function

fun signUpWithEmailAndPassword(email: String, password: String): FirebaseUser? {
    signOutCurrentUser()
     firebaseAuth.createUserWithEmailAndPassword(email, password)
    return firebaseAuth.currentUser
}

the update user function更新用户 function

fun updateUser(user: FirebaseUser?, name: String) {
    val profileUpdates = UserProfileChangeRequest.Builder().apply {
        displayName = name
    }.build()
    user?.updateProfile(profileUpdates)?.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Timber.d("User profile updated.")
        }
    }
}

the sign out current user function注销当前用户 function

private fun signOutCurrentUser() {
    firebaseAuth.currentUser?.let {
        firebaseAuth.signOut()
    }
}

The issue is that user is Added successfully but firebaseAuth.currentUser returns null always.问题是用户已成功添加,但firebaseAuth.currentUser总是返回 null 。

Things I have tried:我尝试过的事情:

  • Adding authStateListener添加 authStateListener
  • Adding onSuccessListener添加 onSuccessListener
  • Adding onCompleteListener添加 onCompleteListener

Please help a brother out请大哥帮忙

Creating a user in Firebase is (like most calls involving cloud-based APIs) an asynchronous operation, which may take time to complete.在 Firebase 中创建用户(与大多数涉及基于云的 API 的调用一样)是一个异步操作,可能需要一些时间才能完成。 By the time your return firebaseAuth.currentUser code runs, the user creation has not been completed yet.当您的return firebaseAuth.currentUser代码运行时,用户创建尚未完成。

If you simply the code to run in a single block, with a completion handler, you'll see that it works fine:如果您只是使用完成处理程序在单个块中运行代码,您会发现它工作正常:

firebaseAuth.createUserWithEmailAndPassword(email, password)
  .addOnCompleteListener(this) { task ->
    if (task.isSuccessful) {
        val user = auth.currentUser

        val profileUpdates = UserProfileChangeRequest.Builder().apply {
            displayName = name
        }.build()
        user?.updateProfile(profileUpdates)?.addOnCompleteListener { task ->
            if (task.isSuccessful) {
                Timber.d("User profile updated.")
            }
        }
    } else {
        // If sign in fails, display a message to the user.
        Log.w(TAG, "createUserWithEmail:failure", task.exception)
        Toast.makeText(baseContext, "Authentication failed.",
                Toast.LENGTH_SHORT).show()
        updateUI(null)
    }

    // ...
  }

Also see:另见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM