Value of type 'AuthDataResult' has no member ‘uid’

前端 未结 1 1679
甜味超标
甜味超标 2020-12-22 04:10

I am trying to access a user\'s uid in Firebase Authentication. I created a createUser completion block in my code and at the end of the block I want to check for the user i

相关标签:
1条回答
  • 2020-12-22 04:31

    According to the guide, when using .createUser,

    If the new account was successfully created, the user is signed in, and you can get the user's account data from the result object that's passed to the callback method.

    Notice in the sample, you get back authResult, not a User object. authResult contains some information, including the User. You can get to the User using authResult.user.

    In addition, when calling the method, if successful, the user is already signed in, so there's no reason to sign them in again. I changed the parameter name to authResult from the sample to help eliminate some of the confusion.

    Auth.auth().createUser(withEmail: email, password: password, completion: { authResult, error in
      if let error = error {
         // report error
         return
      } 
      guard let authResult = authResult else { return }
      let firUser = authResult.user
      let newUser = User(uid: firUser.uid, username: username, fullName: fullName, bio: "", website: "", follows: [], followedBy: [], profileImage: self.profileImage)
      newUser.save(completion: { (error) in
        if let error = error {
          // report
        } else {
          // not sure what you need to do here anymore since the user is already signed in
        }
      })
    })
    
    0 讨论(0)
提交回复
热议问题