我想在我的应用程序中实现一个多用户系统,有管理员用户,没有管理员用户.管理员用户可以在屏幕上看到一些其他用户看不到的功能.我正在try 使用Jetpack Compose和Firebase auth/FiRestore来做这件事,我使用了一个带有一些变量的UserData数据类,其中一个变量是"Role",我在这里输入了用户角色:"User"或"Admin".我不知道这是否是实施的最佳 Select ,欢迎提出建议.我在这里做的是在我的Use repo中添加一个Firebase Auth.AuthStateListener,当它发生变化时,如果用户不为空,我将签入Firebase角色并返回True或False,如果用户为空,则返回False.
(代码)
override fun getUserRole(viewModelScope: CoroutineScope) = callbackFlow {
val authStateListener = FirebaseAuth.AuthStateListener { auth ->
auth.currentUser?.uid?.let {
usersRef.document(it).get().addOnSuccessListener { document ->
if (document != null) {
val userRole = document.get("role").toString() == "ADMIN"
trySend(userRole)
}
}
} ?: trySend(false)
}
auth.addAuthStateListener(authStateListener)
awaitClose {
auth.removeAuthStateListener(authStateListener)
}
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), false)
我不知道如何实现多用户系统,我试过了,需要帮助来做这件事.
我想实现的另一个规范是从FiRestore获取用户数据,我将需要所有应用程序中的数据,但我不想在每个视图模型中判断Auth.CurrentUser(如果是空的或不是..),并根据用户状态在每个视图模型中获取数据.我能做些什么呢?
谢谢!