0

i'm writing an ios-app with firebase as backend. In my app, there are some admins, who should be able to create new users. A new user should not be able to create an account on his own.

On web, i'm using the function

ref.createUser({
        email: ctrl.user.email,
        password: ctrl.user.password
    }, function(error, userData) { // }

with a random password and reset the password afterwards. So the user will receive an email with a password reset link and can set a new password and log in after that. After creating the user, i'm storing all relevant userdata to the database.

On ios i'm trying to create a new user with:

FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in }

But if I do it this way, I'm signed out with my admin-account and logged in with my fresh created account. Due to this, I'm not able, to store the userdata to the database, because this is restricted to admins....

Is there a way on ios to only create an user, without to auto-signin with this user?

Best wishes

Tobi

EDIT I've written an AWS lambda function with the firebase admin sdk for node.js and create the user inside the function and trigger the function via a webservice-call from my ios-app. Not perfect, but working....

Tobi
  • 21
  • 3

1 Answers1

0

Yes, calling createUser(withEmail:, password:) will log in a new user. To achieve what you are trying to do, I recommend a workaround by doing something like this (assume that currently an Admin is logged in):

// Create the values
var email = alreadySelectedUserEmail
var password = arc4random_uniform(1000000) // or another random number

// Send the information to the database
FIRDatabase.database().reference().child("user_data").child(email) .
        setValue("lorem ipsum") // or another value

//  THEN create the user, AFTER sending the data
        // This will automatically log you in to the new account
FIRAuth.auth()?.createUser(withEmail: email,
        password: password) { (user, error) in
    // Check for any errors
    if let error = error {
        print error
    } else {
        // If there are none, LOG OUT…
        try! FIRAuth.auth()!.signOut()

        // …and log back into Admin (with whatever login info they have)
        FIRAuth.auth()?.signIn(withEmail: adminEmail,
                password: adminPassword) { (user, error) in
            // Check for any errors
            if let error = error {
                print error
            } else {
                // All done, new account created!
            }
    }
}
Marco
  • 2,004
  • 15
  • 24
  • Have had a similar idea, but how do I get the adminPassword? I could store it at the initial admin-login as a UserDefault-Value, but what, if the admin changes his password otherwise? – Tobi Nov 24 '16 at 17:10
  • Now, I have written an AWS Lambda function with the Firebase Admin SDK for Node.JS and create the user with this over a webservice-call from the ios-app. – Tobi Nov 24 '16 at 20:14