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 ?
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 ?
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 = "service-account@my-project-abc123.iam.gserviceaccount.com"
$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
Yes, you can with admin sdk:
admin
.auth()
.createUser({
uid: 'some-uid',
email: 'user@example.com',
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);
});
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);
});