Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 594
Bhawin Parkeria Avatar asked Jul 11 '17 13:07

Bhawin Parkeria


2 Answers

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
like image 104
Markymark Avatar answered Nov 15 '22 00:11

Markymark


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);
  });
like image 31
Enrique David Calatayud Avatar answered Nov 14 '22 23:11

Enrique David Calatayud