如何在 Firebase 中退出应用程序后保持用户会话
How to maintain user session after exiting app in Firebase
我有一个使用 Firebase 作为后端的应用程序。我的应用程序使用简单的电子邮件和密码登录来实现 Firebase-Authentication API 进行身份验证。
除一个问题外,一切正常。即使用户关闭应用程序,我也想保持用户会话。
FIRAuth.auth()?.currentUser?具有当前用户的所有用户属性(例如:uid、电子邮件、令牌等...),效果很好。我注意到的问题是 FIRAuth.auth()?.currentUser?不是持久的。所以在退出应用程序并再次启动后,它 returns null.
我不想在用户每次打开应用程序时都要求他们重新登录。有解决办法吗?
Firebase 应该已经通过应用程序启动来跟踪您的用户。
您的问题可能是直接使用 FIRAuth.auth()?.currentUser?
而不是推荐的方法。
The recommended way to get the current user is by setting a listener
on the Auth object:
FIRAuth.auth()?.addStateDidChangeListener { auth, user in
if let user = user {
// User is signed in.
} else {
// No user is signed in.
}
}
By using a listener, you ensure that the Auth object isn't in an
intermediate state—such as initialisation—when you get the current
user.
在我自己的应用程序中,我同时使用了两者。如果使用第一种方法的用户不存在,它将转到第二种方法,如果失败,则显示 login/signup 屏幕。
FirAuth 已重命名为 Auth
请使用最新的 Firebase pod
我有一个使用 Firebase 作为后端的应用程序。我的应用程序使用简单的电子邮件和密码登录来实现 Firebase-Authentication API 进行身份验证。
除一个问题外,一切正常。即使用户关闭应用程序,我也想保持用户会话。
FIRAuth.auth()?.currentUser?具有当前用户的所有用户属性(例如:uid、电子邮件、令牌等...),效果很好。我注意到的问题是 FIRAuth.auth()?.currentUser?不是持久的。所以在退出应用程序并再次启动后,它 returns null.
我不想在用户每次打开应用程序时都要求他们重新登录。有解决办法吗?
Firebase 应该已经通过应用程序启动来跟踪您的用户。
您的问题可能是直接使用 FIRAuth.auth()?.currentUser?
而不是推荐的方法。
The recommended way to get the current user is by setting a listener on the Auth object:
FIRAuth.auth()?.addStateDidChangeListener { auth, user in if let user = user { // User is signed in. } else { // No user is signed in. } }
By using a listener, you ensure that the Auth object isn't in an intermediate state—such as initialisation—when you get the current user.
在我自己的应用程序中,我同时使用了两者。如果使用第一种方法的用户不存在,它将转到第二种方法,如果失败,则显示 login/signup 屏幕。
FirAuth 已重命名为 Auth
请使用最新的 Firebase pod