Reputation: 1
My goal is to link two users together via the unique UID of the first user (in other words User B belongs to User A).
I took the following steps:
I want to store the User information (UID, Email, Password) of the new user (B) together with the UID of the loggedin user (A), so that the result in Firebase looks something like this:
root
-superusers
-Bl41OxkohiNFc3
-email: [email protected]
-password: test
-simpleusers
-Vl21OxkohiFFc3HQ
-superuserUID: Bl41OxkohiNFc3
-email: @example.de
-password: test
Im having problems with step Nr.4. Xcode/Firebase won't save the UID of user A, it always saves the UID of user B in superuserUID.
Here is my code for Step4:
@IBAction func RegisterSimpleUser(_ sender: Any) {
FIRAuth.auth()?.createUser(withEmail: autoemail, password: autopassword, completion: {(user, error) in
if error != nil {
print(error!.localizedDescription)
} else {
print ("User created")
}
let userID: String = user!.uid
let userEmail: String = simepleuser_email
let userPassword: String = simpleuser_password
let SuperUserID: String = (FIRAuth.auth()?.currentUser?.uid)!
//simepleuser_email, simpleuser_password coming from Text Fiels
ref.child("simpleusers").child(userID).setValue(["SuperUserID": SuperUserID, "Email": userEmail, "Password": userPassword])
}
What am I doing wrong? Can anybody help me or point me in the right direction? I'm new to Firebase/Swift. Thanks.
Upvotes: 0
Views: 243
Reputation: 35657
You can't actually do that from the client: Once a new user is created, they are automatically authenticated and the current user is unauthenticated.
However, you can write the current uid to Firebase before creating the new user or capture the current uid in a 'global' var so you can store it once the new user is created in the structure from the original question.
Another option would be to have the admin user be notified when a particular client 'signs up' - this could be monitoring a 'new user node' for a particular email. Once the admin is notified of the new user, they will be presented with the uid and then can write out a corresponding node.
Check out this post Firebase kicks out current user which contains good info related to the topic and updated info where Firebase added some server functionality that may help.
Hopefully Firebase will provide another option for creating a user from the client in the future as creating an 'admin' user that can create other users is a lot more challenging that it should be.
Upvotes: 1