Firebase Auth lastSignInDate和creationDate比较失败 [英] Firebase Auth lastSignInDate and creationDate comparison failure
问题描述
首先,在这种情况下,我需要知道这是第一个用户的登录(登录后注册也将作为第一个用户),因此我可以将其添加到数据库中.为此,我需要进行以下检查:
First of all what I need in this case is knowing that this is the very first user's login (log in-after-sign up also counts as first) so I can add them to the database. For this purpose I have this check:
func isNewUser(userMetadata: UserMetadata) -> Bool {
return userMetadata.creationDate == userMetadata.lastSignInDate && !UserDefaults.standard.hasLoggedIn
}
我需要进行UserDefaults
检查,因为在用户注销之前,creationDate和lastSignInDate都是相等的.
I need the UserDefaults
check because creationDate and lastSignInDate will be equal till the moment the user logs out.
直到今天,当我收到这些奇怪的日志时,一切都运转良好:
Everything worked perfectly till today when I get these strange logs:
(lldb) po userMetadata.creationDate
▿ Optional<Date>
▿ some : 2019-02-06 21:18:15 +0000
- timeIntervalSinceReferenceDate : 571180695.0
(lldb) po userMetadata.lastSignInDate
▿ Optional<Date>
▿ some : 2019-02-06 21:18:15 +0000
- timeIntervalSinceReferenceDate : 571180695.1040001
日期看起来一样,但是对相等性的检查失败.
Dates look the same but the check for equality fails.
我在其中调用isNewUser
的代码:
authListener = Auth.auth().addStateDidChangeListener { [unowned self] (auth, user) in
if let firebaseUser = user, Auth.auth().isNewUser(userMetadata: firebaseUser.metadata) {
self.handleNewUser(firebaseUser)
}
}
该文档对首次登录没有太多说明.我知道AdditionalUserInfo
类中有一个isNewUser
属性,但是我不知道如何使用它.
The documentation does not say much about first logins. I know there is a isNewUser
property in AdditionalUserInfo
class but I don't know how I can use it.
推荐答案
firebaser此处
这可能与我们正在研究的一些后端更改有关.目前,您可以在比较之前四舍五入timeIntervalSinceReferenceDate
的值.
This might be related to some backend changes, which we're looking into. For the moment you could round down the value of timeIntervalSinceReferenceDate
before the comparison.
但是最好使用 AuthDataResult
中获得该信息,用户登录后就这样,例如使用 signIn(withEmail:password:completion:)
.在FirebaseUI中,您还可以获取AuthDataResult
,如下所示: https://github.com/firebase/FirebaseUI-iOS/tree/master/Auth#sign-in
But it's better to use AdditionalUserInfo.newUser
for this check these days. You get that from AuthDataResult
, so straight after the user signs in like for example with signIn(withEmail:password:completion:)
. In FirebaseUI you can also get the AuthDataResult
as shown here: https://github.com/firebase/FirebaseUI-iOS/tree/master/Auth#sign-in
这篇关于Firebase Auth lastSignInDate和creationDate比较失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!