4

I have following code to login in firebase using flutter. This works fine.

FirebaseAuth.instance.signInWithEmailAndPassword(email: myemail, password: mypassword);

After successful authentication, I am creating nw user using below code.

UserCredential userCredentials = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: myemail, password: mypassword);

but, with approach, the current user changes. I want to create the user without changing the current User. Is this possible?

Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • 1
    If the first statement works fine and user logs in, then why are you creating new user with same credentials? – pasty Jan 24 '21 at 09:44
  • Actually, I am trying an admin panel to create users after login. Is that possible with the code in description? – Pankaj Jan 24 '21 at 17:10
  • You are looking for this: https://stackoverflow.com/questions/54412712/flutter-firebase-authentication-create-user-without-automatically-logging-in – Andrej Jan 24 '21 at 20:06

3 Answers3

4

I have found one solution and it's working for me

In main dart file initializeApp like this

void main() async{
await Firebase.initializeApp();
  runApp(MyApp());
}

Create a simple/admin user

FirebaseAuth.instance.signInWithEmailAndPassword(email: myemail, password: mypassword);

Add another user

FirebaseApp app = await Firebase.initializeApp(
        name: 'secondary', options: Firebase.app().options);
await FirebaseAuth.instanceFor(app: app)
        .createUserWithEmailAndPassword(
            email: _email, password: _password);

app.delete();
Gursewak Singh
  • 1,576
  • 1
  • 16
  • 32
3

This is a duplicate of Flutter: Firebase Authentication Create User Without Automatically Logging In, but I can't flag the question as it has an open bounty.

You need to create a second instance of FirebaseApp and create the new user with that instance:

static Future<UserCredential> register(String email, String password) async {
    FirebaseApp app = await Firebase.initializeApp(
        name: 'Secondary', options: Firebase.app().options);
    try {
        UserCredential userCredential = await FirebaseAuth.instanceFor(app: app)
        .createUserWithEmailAndPassword(email: email, password: password);
    }
    on FirebaseAuthException catch (e) {
      // Do something with exception. This try/catch is here to make sure 
      // that even if the user creation fails, app.delete() runs, if is not, 
      // next time Firebase.initializeApp() will fail as the previous one was
      // not deleted.
    }
    
    await app.delete();
    return Future.sync(() => userCredential);
}
Nick Fisher
  • 725
  • 5
  • 13
  • This is great insight Nick and very helpful. I ran into the same problem with an Admin self-service portal for a customer and used the sample above to craft a solution. Is there any documentation that says "Why" this works this way and "why" the creation of a 2nd FirebaseApp is needed? Seems "off" and it should work without the 2nd App creation. Also, I wrapped my App delete in a Finally statement – Martin Palmer Mar 10 '23 at 02:16
0

What you are trying to do is not secure (generating someone else's password) And you are correct in that sense there is no way to create a password for another email, as an admin.

What you want to do is sendSignInLinkTOEmail.

This is just the same procedure to sign up with GSuit and other popular services as well.

In case if you really need to set up users, you have to use a separate auth module (should be easy to write your own) and not firebase, and then link the users together when they sign up. It's a hacky way and I don't recommend it.

Prabhakar Poudel
  • 339
  • 7
  • 19