【问题标题】:iOS/Swift/Firebase Authentication: Help Needed on Accessing "isNewUser"iOS/Swift/Firebase 身份验证:访问“isNewUser”需要帮助
【发布时间】:2018-11-25 15:38:54
【问题描述】:

在我的 iOS/Swift/Firebase 应用程序中,我试图在用户通过电子邮件/密码成功登录后访问“isNewUser”参数,以便我可以弹出一个窗口强制他们从临时密码重置密码最初在创建用户时分配。

任何见解将不胜感激。谢谢。

【问题讨论】:

  • 是否有 ISNewUser 参数?在 Firebase 文档中找不到它
  • 没有。要检测用户是否是新用户,请比较 the user's metadata 中的 lastSignInDate 和 creationDate 属性。
  • 我在 Firebase 材料中的“FirebaseAuth 框架参考”下找到了“AdditionalUserInfo”的参考,根据材料“指示当前用户是否首次登录。”
  • 代码的 sn-p 给出“init() 不可用”错误,是:Auth.auth().signIn(withEmail: emailAddress!, password: accountPassword!, completion: { (user, error) in if error == nil { if (AuthDataResult().additionalUserInfo?.isNewUser)! { self.forceNewPassword() } else { self.signinSuccess() } } else { let errorCode = (error! as NSError).code self.signinFailure(errorCode) } })

标签: ios swift firebase-authentication


【解决方案1】:

.isNewUser Bool 可从 FirebaseAuth AdditionalUserInfo 类获得。 Here is the link。为了使用此代码,请查看我在下面编写的演示登录功能。

Auth.auth().signIn(with: credential) { (result, error) in
        
        if let error = error {
            print("Error: \(error)");
            return;
            
        }
        
        // Fetch the user's info
        guard let uid = result?.user.uid else {return}
        
        // Safely unwrap the boolean value rather than forcing it with "!" which could crash your app if a nil value is found
        guard let newUserStatus = result?.additionalUserInfo?.isNewUser else {return}

        // Test the value
        print("\nIs new user? \(newUserStatus)\n")
        
        if newUserStatus == true {
            // Provide your alert prompt

        }
        else{
            // Transition view to continue into the app

        }
    }

【讨论】:

  • additionalUserInfo 什么时候会为零?
猜你喜欢
  • 2012-06-03
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 2020-07-25
  • 1970-01-01
  • 2012-03-18
  • 2011-09-18
相关资源
最近更新 更多