如何实现管理员和用户的多重自动登录?有两种类型的帐户,首先是买方帐户(Dashboard1)和卖方帐户(仪表板)
登录和关闭后,应用程序将引用仪表板活动。应该做出什么改变,使其指向特定的活动。
val currentUserID1 = FirestoreClass().getCurrentUserID1()
val currentUserID = FirestoreClass().getCurrentUserID()
if (currentUserID1.isNotEmpty()) {
// Launch dashboard screen.
startActivity(Intent(this@Splash1Activity, Dashboard1Activity::class.java))
} else if (currentUserID.isNotEmpty()&¤tUserID1.isEmpty()) {
// Launch dashboard screen.
startActivity(Intent(this@Splash1Activity, DashboardActivity::class.java))
}else {
// Launch the Login Activity
startActivity(Intent(this@Splash1Activity, Login1Activity::class.java))
}
finish() // Call this when your activity is done and should be closed.
/**
* A function to get the user id of current logged user.
*/
fun getCurrentUserID(): String {
// An Instance of currentUser using FirebaseAuth
val currentUser = FirebaseAuth.getInstance().currentUser
// A variable to assign the currentUserId if it is not null or else it will be blank.
var currentUserID = ""
if (currentUser != null) {
currentUserID = currentUser.uid
}
return currentUserID
}
fun getCurrentUserID1(): String {
// An Instance of currentUser using FirebaseAuth
val currentUser = FirebaseAuth.getInstance().currentUser
// A variable to assign the currentUserId if it is not null or else it will be blank.
var currentUserID = ""
if (currentUser != null) {
currentUserID = currentUser.uid
}
return currentUserID
}错误日志:
2021-10-22 12:07:44.259 10095-10095/com.shop.shopkar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shop.shopkar, PID: 10095
java.lang.NullPointerException
at com.android.shopkar.firestore.FirestoreClass.getUserDetails1$lambda-6(FirestoreClass.kt:182)
at com.android.shopkar.firestore.FirestoreClass.$r8$lambda$Zke3QkRIZzRRvIj5tck_XAhIVPs(FirestoreClass.kt)
at com.android.shopkar.firestore.FirestoreClass$$ExternalSyntheticLambda37.onSuccess(Unknown Source)
at com.google.android.gms.tasks.zzn.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)发布于 2021-10-22 06:39:08
假设SplashActivity是启动应用程序时显示的第一个活动,那么您共享的代码将根据身份验证状态将用户重定向到DashboardActivity或LoginActivity。但这并不完全正确,因为您正在检查UID是否为空。要知道用户是否已登录,您应该检查是否为空:
val auth = FirebaseAuth.getInstance()
val currentUser = auth.currentUser
if(currentUser == null) {
startActivity(Intent(this@SplashActivity,LoginActivity::class.java))
} else {
startActivity(Intent(this@SplashActivity, DashboardActivity::class.java))
}但是,此机制仅在用户打开应用程序时才起作用并重定向用户。
在使用应用程序时注销如何?
在这种情况下,SplashActivity中现有代码没有任何帮助。因此,要知道用户何时发出声音,您必须使用一个侦听器,该侦听器应该添加到DashboardActivity中,并且应该如下所示:
val authStateListener = AuthStateListener { auth ->
val firebaseUser = auth.currentUser
if (firebaseUser == null) {
val intent = Intent(this@DashboardActivity, LoginActivity::class.java)
startActivity(intent)
finish()
}
}由于它是一个侦听器,您需要根据活动的生命周期添加和删除它。因此,首先必须在DashboardActivity中创建一个FirebaseAuth对象:
val firebaseAuth = FirebaseAuth.getInstance()然后在onStart()中附加监听程序
override fun onStart() {
super.onStart()
firebaseAuth.addAuthStateListener(authStateListener)
}并相应地在onStop()中将其删除
override fun onStop() {
super.onStop()
firebaseAuth.removeAuthStateListener(authStateListener)
}由于您使用的是Kotlin编程语言,因此我建议您检查:
它包含了您在一个干净的MVVM架构中寻找的确切机制。这是一个为学习目的而设计的应用程序。
附注:不需要任何处理程序。
https://stackoverflow.com/questions/69671068
复制相似问题