【问题标题】:How can i create a user with info about Display Name, username & Profile pic?如何创建具有显示名称、用户名和个人资料图片信息的用户?
【发布时间】:2016-06-06 04:16:31
【问题描述】:

如何创建包含显示名称、用户名和个人资料图片信息的用户?

我可以使用此代码吗?

if let user = FIRAuth.auth()?.currentUser
let name = user.displayName
let email = user.email
let photoUrl = user.photoURL
let uid = user.uid; // The user's ID, unique to the Firebase project.
                    // Do NOT use this value to authenticate with
                    // your backend server, if you have one. Use
                    // getTokenWithCompletion:completion: instead.
} else {
  // No user is signed in.
}

如果不是,此代码用于什么目的? 哪些代码可用于确定用户名、个人资料图片等?

我是 Firebase 的新手。

【问题讨论】:

    标签: ios firebase swift2 firebase-authentication


    【解决方案1】:

    Firebase 使用 FIRAuth 类/API 为用户提供身份验证

    但是,它们无法使用 Firebase 数据库等自定义字段进行编辑。

    要允许用户自定义字段,您需要在数据库中复制具有相同唯一 uid 的用户,您在用户的身份验证中收到。像这样的

    if let user = FIRAuth.auth()?.currentUser {
      let name = user.displayName
      let email = user.email
      let photoUrl = user.photoURL
      let uid = user.uid; 
    
      let ref = FIRDatabase.database().reference()
      let key = uid
    
            let userDict = [ "name"    : name ,
                             "email"   : email,
                         "photoUrl"    : photoUrl]
    
            let childUpdates = ["/users/\(key)": userDict]
            ref.updateChildValues(childUpdates, withCompletionBlock: { (error, ref) -> Void in
                  // now users exist in the database
            })
    } 
    

    我们将它推送到数据库中的users 端点,并使用uid 键通过以下行/users/\(key)

    【讨论】:

    • 下面代码的作用是什么?在 FIRDatabase 之后?
    • 它将用户字典保存在我们数据库中的特定端点users/key
    • 有必要吗?
    • 如果您希望用户拥有自定义字段 - 那么您还需要在数据库中包含用户详细信息。这段代码展示了如何做到这一点。
    • 好的!非常感谢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2023-01-20
    • 2018-09-26
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    相关资源
    最近更新 更多