Firebase Custom Claims with Admin SDK on Node.js

Introduction

service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.token.admin == true;
}
}
}

Prerequisites

The Node Script

var admin = require('firebase-admin');
var serviceAccount = require("./<your-service-account-specific-string-here>.json");
var uid = process.argv[2];

if(uid){
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<your-project-id-here>.firebaseio.com"
});
return admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
console.log("Custom Claim Added to UID.");
process.exit();
}).catch(function(error) {
console.log('Error adding admin:', error);
process.exit();
});
} else {
return console.log("uid argument required");
process.exit();
}

--

--

Delivering refined solutions via vigorous practice. Tulane ('97), Cisco CCIE# 6824, Google Certified Professional Cloud Architect, and USPA Master Skydiver

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ronnie Royston

Delivering refined solutions via vigorous practice. Tulane ('97), Cisco CCIE# 6824, Google Certified Professional Cloud Architect, and USPA Master Skydiver