Firebase Auth lastSignInDate和creationDate比较失败 [英] Firebase Auth lastSignInDate and creationDate comparison failure

查看:82
本文介绍了Firebase Auth lastSignInDate和creationDate比较失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,在这种情况下,我需要知道这是第一个用户的登录(登录后注册也将作为第一个用户),因此我可以将其添加到数据库中.为此,我需要进行以下检查:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆