Pedro Jardino
Pedro Jardino

Reputation: 1

get user name from firebase xcode

Trying to get a user name from firebase and then displayed on a label. Any help? heres a sample of the code.

import Firebase
class HomeViewController: UIViewController {
  @IBOutlet weak var userName: UILabel!
   
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
                    
  }
   
  func getUserName(_ message:String) {
    let uid = Auth.auth().currentUser?.uid
    Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
       
      if let dictionary = snapshot.value as? [String: AnyObject] {
        self.userName.text = dictionary["firstname"] as? String         
      }
       
    })
  }
}

Upvotes: 0

Views: 453

Answers (3)

Zealous System
Zealous System

Reputation: 2324

You have To try this code...(After Authentication , You have to create display name ("Firstname + Lastname") inside mobile app using below mentioned code )

 if let currentUser = Auth.auth().currentUser {
    let changeRequest = currentUser.createProfileChangeRequest()
    changeRequest.displayName = "Firstname" + "Lastname"
    changeRequest.commitChanges(completion: { (error) in
        if let error = error {
            print("--> firebase user display name error:- ", error)
        }
    })
}

After completion of this code we got current user displayname.

Upvotes: 0

Zealous System
Zealous System

Reputation: 2324

Swift

let currentUserName = Auth.auth().currentUser?.displayName

print(currentUserName)

Upvotes: 2

Joeman
Joeman

Reputation: 251

user.displayName It's in the firebase docs

Upvotes: 1

Related Questions