0

How can I create a user in Firebase auth with a custom UID for me. In my app, I am using Email and Password to create the users. So I also use the method:

try {
  UserCredential userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: "barry.allen@example.com",
    password: "SuperSecretPassword!"
  );
} on FirebaseAuthException catch (e) {
  if (e.code == 'weak-password') {
    print('The password provided is too weak.');
  } else if (e.code == 'email-already-in-use') {
    print('The account already exists for that email.');
  }
} catch (e) {
  print(e);
}

Is there any way for me to provide the UID and keep better control of my users?

Something like (In the same method):

try {
  UserCredential userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: "barry.allen@example.com",
    password: "SuperSecretPassword!"
    //uid: "My own id"
  );
} on FirebaseAuthException catch (e) {
  if (e.code == 'weak-password') {
    print('The password provided is too weak.');
  } else if (e.code == 'email-already-in-use') {
    print('The account already exists for that email.');
  }
} catch (e) {
  print(e);
}

What can I do here?

JSON
  • 3
  • 1

1 Answers1

1

I don't believe you can do it via the FlutterFire SDK, but through the Firebase Admin SDK (i.e. via a Cloud Function), and if you do, you have to use the generated token to authenticate.

https://firebase.google.com/docs/auth/admin/create-custom-tokens#create_custom_tokens_using_the_firebase_admin_sdk

Alternative: Since Email and Password Authentication already give you a unique UID, that UID is pretty strong for any user management, but in case you want to come up with your own, you could generate another one (still keeping the one from Firebase Email and Pwd Auth - just for reference purposes) and when adding your users to another collection, you could do like:

FirebaseFirestore.instance.collection('users').add({
  'uid': userCredential.user.uid,
  'sUid': 'YOUR_GENERATED_ONE',
  'email': 'email',
  // other user info
});

Something of that nature. My two cents.

Roman Jaquez
  • 2,499
  • 1
  • 14
  • 7
  • Okay bro, but, what would the document be called? Since we will need it to access the user information – JSON Feb 22 '22 at 23:26
  • well, the collection will be called "users" and the document would just have an automatically assigned ID as soon as you add it to the "users" collection. Cuz then you could just do a query like: FirebaseFirestore.instance.collection('users').where('sUid', isEqualTo: 'YOUR_UNIQUE_ID').get(); or even fetch the document from the original uid; is just another way to manage your users - whatever works best in your case. – Roman Jaquez Feb 22 '22 at 23:28
  • Okay bro, thanks. Thanks bro, I think it worked – JSON Feb 23 '22 at 00:29
  • Sweet - glad it did. Let me know so we can update the answer and mark it correct. – Roman Jaquez Feb 23 '22 at 00:32