Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Firebase Rules sample app
var admin = require('firebase-admin');
var serviceAccount = require("/home/srashid/Desktop/firebase_gcs/fb-svc-account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
apiKey: "AIzaSyDS32ruqLyTQGSoF",
authDomain: "sa-broker.firebaseapp.com",
databaseURL: "https://sa-broker.firebaseio.com",
projectId: "sa-broker",
storageBucket: "sa-broker.appspot.com",
messagingSenderId: "653262156156",
appId: "1:653262156156:web:6e8c116d19d9"
});
var arg = process.argv.slice(2);
switch (arg[0]) {
case 'updateuser':
if (arg.length < 2) {
console.log("Usage: node updateuser <email> <groupIdvalue>")
process.exit(1)
}
email = arg[1];
groupIdValue = arg[2]
admin.auth().getUserByEmail(email)
.then(userRecord => {
console.log('Successfully fetched user data:', userRecord.toJSON());
// set admin=true and groupId=12345
admin.auth().setCustomUserClaims(userRecord.uid, {admin: true, groupId: groupIdValue}).then(() => {
console.log("Custom Claim set");
process.exit(0);
});
})
.catch(error => {
console.log('Error fetching user data:', error);
process.exit(1);
});
break;
case 'upload':
if (arg.length < 3) {
console.log("Usage: node upload <filename> <destinationFile> <ownerValue>")
process.exit(1)
}
filename = arg[1];
destinationFile = arg[2]
ownerValue = arg[3]
var bucket = admin.storage().bucket();
var metadata = {
owner: ownerValue
};
var options = {
destination: destinationFile,
resumable: false,
metadata: {
metadata: metadata
}
};
bucket.upload(filename, options, function(err, remoteFile) {
if (!err) {
console.log("file uploaded!");
process.exit(0);
} else {
console.log(err);
process.exit(1);
}
});
break;
default:
console.log('Sorry, argument must be either createuser or upload');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment