Firebase 客户端无权执行此操作
Posted
技术标签:
【中文标题】Firebase 客户端无权执行此操作【英文标题】:Firebase Client Does Not Have Permission to Perform This Operation 【发布时间】:2020-12-12 00:55:29 【问题描述】:我在我的应用中使用 Firebase 身份验证,并且我有以下代码允许用户退出:
FirebaseAuth.getInstance().signOut()
userInfo.removeEventListener(listener)
val intent = Intent(this@SettingsFragment.context, LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
代码成功注销了用户,但我得到了这个写着The client does not have permission to perform this operation
的吐司。我为什么要吃这个吐司?如何防止它发生?
更新 1: Firebase 实时数据库代码和事件监听器的移除
下面我包含了使用 Firebase 实时数据库的代码:
userInfo = database.getReference("users").child(auth.currentUser!!.uid)
listener = userInfo.addValueEventListener(object : ValueEventListener
override fun onDataChange(dataSnapshot: DataSnapshot)
for (postSnapshot in dataSnapshot.children)
if (postSnapshot.key == "firstName")
firstNamePreference.text = postSnapshot.getValue(String::class.java)
else if (postSnapshot.key == "lastName")
lastNamePreference.text = postSnapshot.getValue(String::class.java)
else if (postSnapshot.key == "username")
userNamePreference.title =
"Username: $postSnapshot.getValue(String::class.java)"
override fun onCancelled(databaseError: DatabaseError)
Toast.makeText(
context, databaseError.message,
Toast.LENGTH_LONG
).show()
)
我还在我的退出代码中包含了userInfo.removeEventListener(listener)
,但我仍然收到错误消息。
更新 2:Logcat 处于调试模式,用于注销用户
D/InputTransport: Input channel destroyed: fd=77
D/ViewRootImpl@b3aa7b8[SettingsActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
D/InputMethodManager: prepareNavigationBarInfo() DecorView@6d11dc3[SettingsActivity]
D/InputMethodManager: getNavigationBarColor() -855310
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@f47f5fd
W/SyncTree: Listen at /users/ezTJRHVKZXhHyMGQGGljdQpfzGv1 failed: DatabaseError: This client does not have permission to perform this operation
I/DecorView: createDecorCaptionView >> DecorView@aa5b516[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true
D/InputTransport: Input channel constructed: fd=78
D/ViewRootImpl@46f9f6b[LoginActivity]: setView = DecorView@aa5b516[LoginActivity] TM=true MM=false
D/ViewRootImpl@46f9f6b[LoginActivity]: dispatchAttachedToWindow
D/ViewRootImpl@46f9f6b[LoginActivity]: Relayout returned: old=[0,0][1440,2960] new=[0,0][1440,2960] result=0x7 surface=valid=true 480479002624 changed=true
D/OpenGLRenderer: eglCreateWindowSurface = 0x6fdfef7280, 0x6fdec7c010
D/ViewRootImpl@46f9f6b[LoginActivity]: MSG_RESIZED: frame=Rect(0, 0 - 1440, 2960) ci=Rect(0, 96 - 0, 192) vi=Rect(0, 96 - 0, 192) or=1
D/ViewRootImpl@46f9f6b[LoginActivity]: MSG_WINDOW_FOCUS_CHANGED 1 1
D/InputMethodManager: prepareNavigationBarInfo() DecorView@aa5b516[LoginActivity]
D/InputMethodManager: getNavigationBarColor() -855310
D/InputMethodManager: prepareNavigationBarInfo() DecorView@aa5b516[LoginActivity]
D/InputMethodManager: getNavigationBarColor() -855310
D/InputMethodManager: startInputInner - Id : 0
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: fd=89
D/InputTransport: Input channel destroyed: fd=97
D/FA: Connected to remote service
D/InputTransport: Input channel constructed: fd=92
D/ViewRootImpl@346cbca[Toast]: setView = android.widget.LinearLayout75c95b1 V.E...... ......I. 0,0-0,0 TM=true MM=false
D/ViewRootImpl@346cbca[Toast]: dispatchAttachedToWindow
D/ViewRootImpl@346cbca[Toast]: Relayout returned: old=[0,96][1440,2768] new=[115,2282][1324,2512] result=0x7 surface=valid=true 480480571392 changed=true
D/OpenGLRenderer: eglCreateWindowSurface = 0x6fdf199d80, 0x6fdedfb010
D/ViewRootImpl@346cbca[Toast]: MSG_RESIZED: frame=Rect(115, 2282 - 1324, 2512) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
D/OpenGLRenderer: eglDestroySurface = 0x6fdfff3d00, 0x6fdfd7c000
D/ViewRootImpl@b3aa7b8[SettingsActivity]: Relayout returned: old=[0,0][1440,2960] new=[0,0][1440,2960] result=0x5 surface=valid=false 0 changed=true
D/ViewRootImpl@b3aa7b8[SettingsActivity]: setWindowStopped(true) old=false
D/ViewRootImpl@b3aa7b8[SettingsActivity]: Surface release. android.view.WindowManagerGlobal.setStoppedState:669 android.app.Activity.performStop:7647 android.app.ActivityThread.callActivityOnStop:4379 android.app.ActivityThread.performStopActivityInner:4357 android.app.ActivityThread.handleStopActivity:4432 android.app.servertransaction.TransactionExecutor.performLifecycleSequence:192 android.app.servertransaction.TransactionExecutor.cycleToPath:165 android.app.servertransaction.TransactionExecutor.executeLifecycleState:142
D/ViewRootImpl@b3aa7b8[SettingsActivity]: dispatchDetachedFromWindow
D/ViewRootImpl@b3aa7b8[SettingsActivity]: Surface release. android.view.ViewRootImpl.doDie:7967 android.view.ViewRootImpl.die:7935 android.view.WindowManagerGlobal.removeViewLocked:497 android.view.WindowManagerGlobal.removeView:435 android.view.WindowManagerImpl.removeViewImmediate:124 android.app.ActivityThread.handleDestroyActivity:4753 android.app.servertransaction.DestroyActivityItem.execute:39 android.app.servertransaction.TransactionExecutor.executeLifecycleState:145
D/InputTransport: Input channel destroyed: fd=91
D/OpenGLRenderer: eglDestroySurface = 0x6fdf199d80, 0x6fdedfb000
D/ViewRootImpl@346cbca[Toast]: dispatchDetachedFromWindow
D/ViewRootImpl@346cbca[Toast]: Surface release. android.view.ViewRootImpl.doDie:7967 android.view.ViewRootImpl.die:7935 android.view.WindowManagerGlobal.removeViewLocked:497 android.view.WindowManagerGlobal.removeView:435 android.view.WindowManagerImpl.removeViewImmediate:124 android.widget.Toast$TN.handleHide:1110 android.widget.Toast$TN$1.handleMessage:898 android.os.Handler.dispatchMessage:106
D/InputTransport: Input channel destroyed: fd=92
【问题讨论】:
【参考方案1】:听起来您有一个 Firebase 数据库(实时数据库或 Cloud Firestore)的侦听器,并且此侦听器要求用户经过身份验证。当您将用户注销时,侦听器将变为无效,并被 Firebase 取消并显示您看到的错误消息。
解决方案是在用户退出之前移除监听器。
【讨论】:
我的活动中确实有一个 Firebase 实时数据库。如何在用户退出之前删除监听器? 您可以删除一个监听器,如下所示:firebase.google.com/docs/database/android/… 我更新了我的帖子以显示 Firebase 实时数据库的使用,包括userInfo.removeEventListener(listener)
,但我仍然收到错误。
在这种情况下听起来你仍然有一个听众。您可以编辑您的问题以显示完整的错误消息和堆栈跟踪吗?
嘿@TomDarious 有这方面的消息吗?你找到额外的听众了吗?如果没有,您能否编辑您的问题以显示完整的错误消息和堆栈跟踪?以上是关于Firebase 客户端无权执行此操作的主要内容,如果未能解决你的问题,请参考以下文章
Firebase 云功能“您的客户端无权从此服务器获取 URL /200”
Flutter Firebase 实时数据库:“[permission-denied] 客户端无权访问所需数据。”问题
对象 ID 为“XXX”的客户端“XXX”无权执行操作“Microsoft.Media/mediaServices/transforms/write”
Fire Storage Exception([firebase_storage/unauthorized] 用户无权执行所需的操作。)(我的规则是允许读/写)