I am currently utilizing Firebase cloud functions in conjunction with Android to create a user with custom claims. However, I have encountered an issue where the custom claims are showing up as null despite following the documentation.
Thank you in advance for any help
Here is the code snippet:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const serviceAccount = require('../serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
exports.createSellerAccount = functions.https.onCall((data, context) => {
const userEmail = data.email;
const userPassword = data.password;
return admin.auth().createUser({
email: userEmail,
password: userPassword
}).then((userRecord) => {
// See the UserRecord reference doc for the contents of userRecord.
const additionalClaims = {
premiumAccount: true
};
admin.auth().createCustomToken(userRecord.uid, additionalClaims)
.then(function (customToken) {
// Send token back to client
})
.catch(function (error) {
console.log("Error creating custom token:", error);
});
return {
sellerAccount: userRecord
}
}).catch((error) => {
// console.log("Error creating new user:", error);
if (error.code === "auth/email-already-exists") {
throw new functions.https.HttpsError('already-exists', error.message);
} else if (error.code === 'auth/invalid-email') {
throw new functions.https.HttpsError('invalid-argument', error.message);
} else {
throw new functions.https.HttpsError('unknown', error.message);
}
});
})