在 kotlin 中实现状态持久化
Implementing State Persistence in kotlin
我已经尝试在我的 android 应用程序中实现状态持久化,以便用户保持登录状态,但我无法理解文档,所以我 运行 在这里寻求帮助。
这是片段:
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
.then(function() {
// Existing and future Auth states are now persisted in the current
// session only. Closing the window would clear any existing state even
// if a user forgets to sign out.
// ...
// New sign-in will be persisted with session persistence.
return firebase.auth().signInWithEmailAndPassword(email, password);
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
});
我已经尝试了很多东西,但我不太明白他们在哪里实例化 firebase
变量。
在 Docs 中有这一行:
This will change the type of persistence on the specified Auth
instance for the currently saved Auth session and apply this type of
persistence for future sign-in requests
但我还是不太明白哪个 Auth 实例,我正在使用 FirebaseAuth UI。
这是我的一些代码:
private fun showSignInOptions() {
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
RC_SIGN_IN
)
}
// FirebaseUI Auth code.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
val response = IdpResponse.fromResultIntent(data)
if (resultCode == Activity.RESULT_OK) {
// Successfully signed in
val user = FirebaseAuth.getInstance().currentUser
Log.d("AUTH", "Sign in SUCCESSFUL. $user")
// ...
} else {
Log.d("AUTH", "Sign in failed.")
// Sign in failed. If response is null the user canceled the
// sign-in flow using the back button. Otherwise check
// response.getError().getErrorCode() and handle the error.
// ...
}
}
}
我正在 onCreate()
给 showSignInOptions()
打电话。
提前感谢您的帮助:D
如果我对你的问题的理解正确,你想让你的用户保持登录状态而不是要求他们登录。如果是这种情况,你需要做的就是检查是否
FirebaseAuth.getInstance().currentUser
returns 空。如果它 returns null 然后你可以初始化 FirebaseAuthUI 以便让他们登录。所以最终产品应该类似于以下代码片段:
val mUser = FirebaseAuth.getInstance().currentUser
mUser?.let{
//do your stuff here
} ?: showSignInOptions()
我已经尝试在我的 android 应用程序中实现状态持久化,以便用户保持登录状态,但我无法理解文档,所以我 运行 在这里寻求帮助。
这是片段:
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
.then(function() {
// Existing and future Auth states are now persisted in the current
// session only. Closing the window would clear any existing state even
// if a user forgets to sign out.
// ...
// New sign-in will be persisted with session persistence.
return firebase.auth().signInWithEmailAndPassword(email, password);
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
});
我已经尝试了很多东西,但我不太明白他们在哪里实例化 firebase
变量。
在 Docs 中有这一行:
This will change the type of persistence on the specified Auth instance for the currently saved Auth session and apply this type of persistence for future sign-in requests
但我还是不太明白哪个 Auth 实例,我正在使用 FirebaseAuth UI。
这是我的一些代码:
private fun showSignInOptions() {
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
RC_SIGN_IN
)
}
// FirebaseUI Auth code.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
val response = IdpResponse.fromResultIntent(data)
if (resultCode == Activity.RESULT_OK) {
// Successfully signed in
val user = FirebaseAuth.getInstance().currentUser
Log.d("AUTH", "Sign in SUCCESSFUL. $user")
// ...
} else {
Log.d("AUTH", "Sign in failed.")
// Sign in failed. If response is null the user canceled the
// sign-in flow using the back button. Otherwise check
// response.getError().getErrorCode() and handle the error.
// ...
}
}
}
我正在 onCreate()
给 showSignInOptions()
打电话。
提前感谢您的帮助:D
如果我对你的问题的理解正确,你想让你的用户保持登录状态而不是要求他们登录。如果是这种情况,你需要做的就是检查是否
FirebaseAuth.getInstance().currentUser
returns 空。如果它 returns null 然后你可以初始化 FirebaseAuthUI 以便让他们登录。所以最终产品应该类似于以下代码片段:
val mUser = FirebaseAuth.getInstance().currentUser
mUser?.let{
//do your stuff here
} ?: showSignInOptions()