gpt4 book ai didi

swift - 当前用户 UID 与身份验证 UID 不同吗?

转载 作者:行者123 更新时间:2023-11-30 11:14:21 32 4
gpt4 key购买 nike

警告:我对一般编码和 xcode 非常陌生

运行 Xcode 8.2

无论如何,我已经保存了我的用户身份验证详细信息、电子邮件密码。我将它们保存在 user/currentUser.UID 下,它提供了用户名和电子邮件。我想检索用户名并将其作为标签文本,但 UID 不同?!

通过数据库抓取用户名,并为用户名设置标签

@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

})

}

然后这是允许用户使用 firebase auth 进行注册的代码,然后将用户名和电子邮件值保存在/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")


}


}

然后在我的控制台输出中

    user id: mrsIBAAl2LVb7FetVxnMwBtMJ562

User ID post guard: 0QkYbdPdH7QdZsdRjck53l4Ekrk2
Snap (message1) Welcome to the Beta!
Snap (username) <null>

我还有一个获取 MOTD 的函数,现在是“欢迎参加测试!”这是我数据库中的一个分支,我在不同的函数中引用它,但我认为这不会产生影响。

最佳答案

Eureka !我发现问题了!

我查看了我的注册流程并测试了它的不同部分,这时我注意到数据库中的 UID 是过去的 UID,即身份验证,因此当创建新的身份验证 UID 时,旧的 UID 就会出现在数据库中。我使用控制台打印查看了流程,发现用户的身份验证和创建是在数据库记录用户之后发生的!因此,为了解决这个问题,我将身份验证放入其自己的函数中,然后在用户通过身份验证后,我调用将用户添加到数据库的函数,这样就可以了!

@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
}

感谢您的所有帮助!我想现在我已经度过了这个坎坷,我将能够以更多的知识继续我的编码!

关于swift - 当前用户 UID 与身份验证 UID 不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51887703/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com