Julliard
Julliard

Reputation: 553

create user with self-specified uid

I am using flutter with firebase to manage my users, and in this link, it says you can specify the uid during user creation: https://firebase.google.com/docs/auth/admin/manage-users#create_a_user

My question: What's the equivalent in dart/ flutter? I understand firebase auto-generates one for you, but in my use case I need to be able to specify mine.

For flutter, I am only aware of createUserWithEmailAndPassword method but it does not have a 'uid' argument.

FirebaseAuth.instance.createUserWithEmailAndPassword(email: null, password: null)

In the link above, however, they provided an example (node.js) with such methods.

admin.auth().createUser({
  uid: 'some-uid',
  email: '[email protected]',
  phoneNumber: '+11234567890'
})
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log('Successfully created new user:', userRecord.uid);
  })
  .catch(function(error) {
    console.log('Error creating new user:', error);
  });

Upvotes: 3

Views: 1176

Answers (3)

Ramazan Sağır
Ramazan Sağır

Reputation: 5173

Use the firebase cloud functions

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();

exports.createUser1 = functions.https.onCall(async (data, _context) => {
  try {
    const user = await admin.auth().createUser({
      uid: data.uid,
      phoneNumber: data.phoneNumber,
      disabled: false,
    }); return {response: user};
  } catch (error) {
    throw new functions.https.HttpsError("failed to create a user");
  }
});

then request from flutter app

{
    "data":
    {
        "uid": "12345678",
        "phoneNumber": "+905378227777",
        "disabled": false
    }
}

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

You can fully control the creation of Firebase Authentication users by implementing a custom provider. But as you can probably imagine, this is a sensitive operation, so the code requires that you have full administrative access to the Firebase project. For that reason, you can't run this type of operation in your Flutter app, but must run it on a trusted environment, such as your development machine, a server you control, or Cloud Functions.

Typically this means:

  1. you'll gather the user's credentials in your Flutter app
  2. send them (securely) to a custom endpoint on the server
  3. and there validate the the user credentials are correct
  4. and use the Admin SDK to create a token for the user
  5. that you then send back securely to the Flutter app

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317372

There is no such option for any of the Firebase the client SDKs on any platform (Android, iOS, web, Flutter, etc).

Firebase Admin is a server SDK, and trusts that the code calling its methods is privileged and running in a secure and trusted environment. This is considered to be safe for inventing UIDs. The client SDKs are run on user devices, which is considered untrusted and could be compromised. In that case, the Firebase Auth product has to come up with an appropriate UID for the user.

Upvotes: 0

Related Questions