1

I have an app where users are supposed to be created only by Admin User's the problem is that when a new user is created in Firebase the app sign's in with the new user information, so the original logged user (Admin User), has to logged out, and log back in to create a new user.

This is my function to create a new User:

void createUser(
    String email,
    String password,
    String nombre,
    String dui,
    DateTime fechaNacimiento,
    String telefono,
    String nombreContacto,
    String telefonoContacto,
    DateTime fechaIngreso,
    String radio,
    File foto,
    String acceso,
  ) async {
    try {
      final auth = FirebaseAuth.instance;

      UserCredential authResult = await auth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );

      //var uploadUid = authResult.user?.uid;

      final ref = FirebaseStorage.instance
          .ref()
          .child('user_images')
          .child(authResult.user!.uid + '.jpg');

      await ref.putFile(foto);

      final url = await ref.getDownloadURL();

      await FirebaseFirestore.instance
          .collection('users')
          .doc(authResult.user!.uid)
          .set({
        'nombre': nombre,
        'dui': dui,
        'fechaNacimiento': fechaNacimiento,
        'telefono': telefono,
        'nombreContacto': nombreContacto,
        'telefonoContact': telefonoContacto,
        'fechaIngreso': fechaIngreso,
        'radio': radio,
        'foto': url,
        'acceso': acceso,
        'uid': authResult.user!.uid,
        'correo': email,
        'contrasena': password,
      });
    } catch (err) {
      print(err);
    }
  }

Any Ideas on what to do to avoid the log in on user creation of the newly created user.

Kind Regards

Rene Alas
  • 481
  • 4
  • 14
  • If you are creating new accounts as an admin, you should consider using Cloud functions with Admin SDK. – Dharmaraj Sep 02 '21 at 14:05
  • Any info on how to create the Cloud Function? – Rene Alas Sep 02 '21 at 14:15
  • Checkout [this answer](https://stackoverflow.com/questions/50946601/creating-a-new-user-via-firebase-cloud-functions-account-created-but-can-not-s). Also [create a user](https://firebase.google.com/docs/auth/admin/manage-users#create_a_user) section of the documentation. – Dharmaraj Sep 02 '21 at 14:17
  • The client-side SDKs of Firebase are not designed to be used for creating administrative functionality like this. While there may be workarounds (such as the one in Peter's answer and the link I'll provide), the recommended approach is to use an Admin SDK (for example through Cloud Functions) and call *that* from your app. See https://stackoverflow.com/questions/37517208/firebase-kicks-out-current-user – Frank van Puffelen Sep 02 '21 at 14:31

1 Answers1

5

The original admin user does not have to be logged out to create a new user. Simply do this.

FirebaseApp secondaryApp = await Firebase.initializeApp(
  name: 'SecondaryApp',
  options: Firebase.app().options,
);

try {
  UserCredential credential = await FirebaseAuth.instanceFor(app: secondaryApp)
      .createUserWithEmailAndPassword(
    email: 'email',
    password: 'password',
  );
  if (credential.user == null) throw 'An error occured. Please try again.';
  await credential.user.sendEmailVerification();
} on FirebaseAuthException catch (e) {
  if (e.code == 'weak-password') {
    return _showError('The password provided is too weak.');
  } else if (e.code == 'email-already-in-use') {
    return _showError('An account already exists for this email.');
  }
} catch (e) {
  return _showError('An error occured. Please try again.');
}
...

// after creating the account, delete the secondary app as below:
await secondaryApp.delete();

The above code will not logout the admin user, the admin user can still continue with normal operations after creating the account.

Peter Obiechina
  • 2,686
  • 1
  • 7
  • 14