ios - 将用户注册到 firebase 并登录(如果已注册)

标签 ios swift firebase firebase-authentication

我有以下代码来将用户注册到 firebase...

FIRAuth.auth()?.createUserWithEmail(email.text!, password: password.text!) {(user, error) in
            if let error = error {
                print(error.localizedDescription)
                if error.localizedDescription == "FIRAuthErrorCodeEmailAlreadyInUse"
                {
                   //sign in here?
                }

            }
            else {
                print("User signed in!")
            }
        } }
    else{
        print("You left email/password empty")
    }

如果用户已经创建,我需要添加什么才能自动登录?

谢谢。

最佳答案

您可以使用 Firebase 提供的任何身份验证方法。例如。对于您的情况,这个最合适:

Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
  // ...
}

Source

编辑

您发布的注册方法的错误响应将告诉您给定的用户是否已存在。在这种情况下,您可以从回调中自动调用signIn方法。

查看official docs ,我看到您应该查找的错误代码是 FIRAuthErrorCodeEmailAlreadyInUse。

关于ios - 将用户注册到 firebase 并登录(如果已注册),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45490376/

相关文章:

Firebase 数据结构和 url

javascript - FCM 推送通知在本地工作,但不在服务器上工作

ios - 如何从 UILabel 中删除可选文本

ios - 转换带分隔符的数字格式,

ios - 将手势识别器添加到导航栏

swift - 是否有与 Swift 3's ' 数据类型等效的 writeToFile?

ios - 如何使用 Swift 将通知数据传递给 View Controller

ios - 如何将JSON数据添加到NSArray

ios - 使用动态高度更改 UITableViewCell 中的 UILabel 文本长度不会正确调整其大小

reactjs - 按集合名称返回的 Firebase 数据排序