Created
May 15, 2020 22:48
-
-
Save salrashid123/79b0d22e7171bd2c20b363b76f9066fe to your computer and use it in GitHub Desktop.
Firebase Rules sample app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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