Reputation: 49
Warning: Im very new to coding in general and xcode
Running Xcode 8.2
Anyways, So I have saved my users authentication details, email password. And I save them under user/currentUser.UID and it gives username and email. I want to retrieve the username and put it as a label text but the UID's are different?!
Grab username through database and set a label to the username
@IBOutlet weak var welcomeHomeUsername: UILabel!
func welcomeHomeAddUsername() {
guard let uid12 = Auth.auth().currentUser?.uid else {
return
}
print("User ID post guard: \(uid12)")
ref.child("users").child(uid12).child("username").observeSingleEvent(of: .value, with: { snapshotWelcome in
print(snapshotWelcome)
let welcomeHomeUser = snapshotWelcome.value as? String
self.welcomeHomeUsername.text = welcomeHomeUser
})
}
Then this is the code to allow users to sign up with firebase auth and then saves the username and email values in /users/(uid)/
@IBAction func signUpButton(_ sender: Any)
{
//Authenticate User//
Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in
if user != nil {
print("User Created")
self.performSegue(withIdentifier: "backLogin", sender: self)
}
else {
print("error no seg")
}
}
//Successfully Authenticated User//
guard let uid = Auth.auth().currentUser?.uid else {
return
}
//Add User to Database//
let ref = Database.database().reference(fromURL: "~~~My Database URL~~~")
let usersReference = ref.child("users").child(uid)
let values = ["username": usernameTextField.text, "email": emailTextField.text]
usersReference.updateChildValues(values) { (err, ref) in
if err != nil {
print("error no user created")
}
if (Auth.auth().currentUser != nil){
print("user id: " + (Auth.auth().currentUser?.uid)!);
return
}
print("Saved user succesfully into Firebase DB")
}
}
Then in my console output
user id: mrsIBAAl2LVb7FetVxnMwBtMJ562
User ID post guard: 0QkYbdPdH7QdZsdRjck53l4Ekrk2
Snap (message1) Welcome to the Beta!
Snap (username) <null>
I also have a function that gets the MOTD which right now is "welcome to the test!" and that is a branch in my database which I reference in a different function but I don't think that should make a difference.
Upvotes: 0
Views: 1130
Reputation: 49
EUREKA! I found the problem!
I looked through the flow of my sign up and was testing different parts of it when I noticed that the UID in the database was the past UID that was the auth, so when a new auth UID was made the old one showed up in the database. I looked through the flow using console prints and found that the authentication and creation of the user was happening AFTER the database was logging the user! So to fix this, I put my authentication in its own function, then at the end of that once the user was authenticated, I called the function that adds the user to the databases and this works!
@IBAction func signUpButton(_ sender: Any)
{
signupTrue = 0
print(signupTrue)
let ref3 = Database.database().reference()
ref3.child("schools").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild(self.schoolNameTextfield.text!){
print("true school exist")
signupTrue = 1
print(signupTrue)
self.authUserFunc()
}
else {
print("false school doesn't exist")
signupTrue = 2
print(signupTrue)
}
})
print("Test ending ref3")
}
func authUserFunc() {
//Authenticate User//
Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in
if user != nil {
print("User Created")
self.performSegue(withIdentifier: "backLogin", sender: self)
self.addToDatabase()
}
else {
print("error no seg")
}
}
//Successfully Authenticated User//
}
func addToDatabase() {
print("Starting add to database")
print("starting signup")
//Add User to School Database//
let ref = Database.database().reference(fromURL: "~~~My Database URL~~~")
let userSchoolReference = ref.child("schools").child(schoolNameTextfield.text!).child("users").child("\(Auth.auth().currentUser!.uid)")
let values = ["username": usernameTextField.text!, "email": emailTextField.text!]
userSchoolReference.updateChildValues(values) { (err, ref) in
if err != nil {
print("error no school user created")
}
if (Auth.auth().currentUser != nil){
print("Saved user succesfully into School Firebase DB")
//print("user id: " + (Auth.auth().currentUser?.uid)!);
return
}
}
//add user to users file//
//let userReference = ref.child("users").child("\(Auth.auth().currentUser!.uid)")
let values2 = ["username": usernameTextField.text!, "email": emailTextField.text!, "password": passwordTextField.text!]
Database.database().reference().child("users").child("\(Auth.auth().currentUser!.uid)").updateChildValues(values2) { (err, ref) in
if err != nil{
print("Error no user created")
return
}
}
print("\(Auth.auth().currentUser?.uid)")
//end func addToDatabase
}
Thank you for all your help! I think now that I am over this bump, ill be able to move forward with my coding with more knowledge!
Upvotes: 1