简体   繁体   English

来自 Firebase 的 .isNewUser 方法总是返回 false

[英].isNewUser method from Firebase always return false

I try to use the isNewUser method of firebase but it returns false all the time.我尝试使用 firebase 的 isNewUser 方法,但它始终返回 false。 According to what I read it only works with accounts created via the Firebase Console which is my case.根据我阅读的内容,它仅适用于通过 Firebase 控制台创建的帐户,这就是我的情况。 The function is called as soon as the user clicks on "connect" and his information is correct. function 一旦用户点击“连接”并且他的信息是正确的,就会被调用。

 func sign_in_handler() {
        if self.email != "" && self.pass != "" {
            Auth.auth().signIn(withEmail: self.email, password: self.pass) { (result, error) in
                if err != nil {
                    self.error = err!.localizedDescription
                    self.alert.toggle()
                    return
                }
                guard let isNew = res?.additionalUserInfo?.isNew else {return}
                print("\nIs new user? \(isNew)\n")
                if newUserStatus == true{
                    UserDefaults.standard.set(true, forKey: "status")
                    NotificationCenter.default.post(name: NSNotification.Name("status"), object: nil)
                }
                else{
                    UserDefaults.standard.set(true, forKey: "status")
                    NotificationCenter.default.post(name: NSNotification.Name("status"), object: nil)
                }
            }
        } else {
            self.error = "Wrong informations."
            self.alert.toggle()
        }
    }

Thanks for your help!谢谢你的帮助!

The isNewUser property is only true when the current sign-in happened at the same time as when the account was created.仅当当前登录与创建帐户同时发生时, isNewUser属性才为真。 So it only happens after a call to createUser(... and never when calling signIn(... .所以它只会在调用createUser(...之后发生,而不会在调用signIn(...时发生。

The reason for that is that it essentially compares the lastSignInDate and creationDate from the metadata of that user, and the former is set when the account is created.原因是它本质上是从该用户的metadata中比较lastSignInDatecreationDate ,而前者是在创建帐户时设置的。 So for user you create in the console, the isNewUser property will actually never be true in your application.因此,对于您在控制台中创建的用户, isNewUser属性在您的应用程序中实际上永远不会为真。

If you want to determine whether the user was recently created, you're better of comparing lastSignInDate and creationDate yourself with whatever you consider "new".如果您想确定用户是否是最近创建的,最好将lastSignInDatecreationDate与您认为“新”的任何内容进行比较。

If you want to determine whether you've seen this user before, you'll typically instead want to track their user ID in some backend storage when you first see it.如果您想确定您以前是否见过此用户,您通常希望在第一次看到该用户时在某个后端存储中跟踪他们的用户 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM