Bhawin Parkeria
Bhawin Parkeria

Reputation: 111

Custom Uid In Firebase CreateUser

My app structure requires me to log in user with a specific Uid.

Can i do that in Firebase Auth.

I mean , If i want to create a user with uid=1. which i will send along with email and password.

Is it possible ?

Upvotes: 10

Views: 9883

Answers (3)

Yes, you can with admin sdk:

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

Upvotes: 6

npfoss
npfoss

Reputation: 490

There is a way to create custom uids with the admin SDK, but I'm not sure about the username/password part: https://firebase.google.com/docs/auth/admin/create-custom-tokens#create_custom_tokens_using_the_firebase_admin_sdk

const uid = 'some-uid';

admin
  .auth()
  .createCustomToken(uid)
  .then((customToken) => {
    // Send token back to client
  })
  .catch((error) => {
    console.log('Error creating custom token:', error);
  });

Upvotes: 1

Markymark
Markymark

Reputation: 2979

You can set UID for the Firebase user if you use a custom token to authenticate. See this https://firebase.google.com/docs/auth/admin/create-custom-tokens#create_custom_tokens_using_a_third-party_jwt_library

Here's the example using Ruby (:uid => uid)

require "jwt"

# Get your service account's email address and private key from the JSON key file
$service_account_email = "[email protected]"
$private_key = OpenSSL::PKey::RSA.new "-----BEGIN PRIVATE KEY-----\n..."

def create_custom_token(uid, is_premium_account)
  now_seconds = Time.now.to_i
  payload = {:iss => $service_account_email,
             :sub => $service_account_email,
             :aud => "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
             :iat => now_seconds,
             :exp => now_seconds+(60*60), # Maximum expiration time is one hour
             :uid => uid,
             :claims => {:premium_account => is_premium_account}}
  JWT.encode payload, $private_key, "RS256"
end

Upvotes: 2

Related Questions