无法在Kotlin中获得Firebase当前用户 [英] Unable to get firebase current user in kotlin
问题描述
我正在将FirebaseAuth集成到我正在构建的Android应用程序中.通过添加SDK,下载并添加google-services.json文件,我已成功将Firebase集成到了应用程序中.
I am integrating FirebaseAuth into an Android app that I am building. I have successfully integrated firebase into the app by adding the SDK, downloading and adding the google-services.json file.
当我尝试注册新用户时出现问题.该用户已添加到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. 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
}
}
authenticationService中的注册功能
the signup function in the authenticationService
fun signUpWithEmailAndPassword(email: String, password: String): FirebaseUser? {
signOutCurrentUser()
firebaseAuth.createUserWithEmailAndPassword(email, password)
return firebaseAuth.currentUser
}
更新用户功能
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.")
}
}
}
退出当前用户功能
private fun signOutCurrentUser() {
firebaseAuth.currentUser?.let {
firebaseAuth.signOut()
}
}
问题在于用户已添加成功,但firebaseAuth.currentUser
始终返回null.
The issue is that user is Added successfully but firebaseAuth.currentUser
returns null always.
我尝试过的事情:
- 添加authStateListener
- 添加onSuccessListener
- 添加onCompleteListener
请帮助一个兄弟
推荐答案
在Firebase中创建用户是一项异步操作(就像大多数涉及基于云的API的调用一样),这可能需要一些时间才能完成.在您的return firebaseAuth.currentUser
代码运行时,尚未完成用户创建.
Creating a user in Firebase is (like most calls involving cloud-based APIs) an asynchronous operation, which may take time to complete. By the time your return firebaseAuth.currentUser
code runs, the user creation has not been completed yet.
如果您简单地将代码与完成处理程序一起在单个块中运行,您会发现它可以正常工作:
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)
}
// ...
}
另请参阅:
- getContactsFromFirebase()方法返回空列表
- Firebase在方法外部检索数据为空
- 从addValueEventListener Firebase中检索字符串
- 在Firebase Listener中设置Singleton属性值 (它显示了使用信号量制作代码块,但在我上次尝试时在Android上不起作用).
- getContactsFromFirebase() method return an empty list
- Firebase retrieve data Null outside method
- Retrieve String out of addValueEventListener Firebase
- Setting Singleton property value in Firebase Listener (which shows using semaphores to make the code block, but which didn't work on Android when I last tried it).
这篇关于无法在Kotlin中获得Firebase当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!