Home > other >  A script to create Firebase user and add claims quickly?
A script to create Firebase user and add claims quickly?

Time:11-09

I was wondering if anyone had a script on hand that can take a path to the admin JSON config file and create users with custom claims?

I've never written a script but guessing something like this

import FirebaseAdmin
import "./config.json"

//maybe gets the userID from the console?
createUser() {
...create user and attach the claims
}

CodePudding user response:

Here is a Node.js script that will create two users and assign them a role claim. You can add as much users as you want, just copy/paste and adapt the const p1 block and add the p<X> promise to the Array.

You run it by calling node thenameofthescriptfile.js

#!/usr/bin/node
const admin = require('firebase-admin');

admin.initializeApp({
    credential: admin.credential.cert("xyz.json")
});

const p1 = admin.auth()
    .createUser({
        email: '...',
        password: '....',
        displayName: '....'
    })
    .then((userRecord) => {
        return admin.auth().setCustomUserClaims(userRecord.uid, { role: "..." })
    })
    .catch((error) => {
        console.log('Error creating new user:', error);
    });

const p2 = admin.auth()
    .createUser({
        email: '...',
        password: '....',
        displayName: '....'
    })
    .then((userRecord) => {
        return admin.auth().setCustomUserClaims(userRecord.uid, { role: "..." })
    })
    .catch((error) => {
        console.log('Error creating new user:', error);
    });

Promise.all([p1, p2]);
  • Related