Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Firebase Storage upload client sample app
var firebase = require("firebase/app");
require("firebase/auth");
require("firebase/storage");
const https = require('https');
global.XMLHttpRequest = require("xhr2");
var firebaseConfig = {
apiKey: "AIzaSyDS32ruqLyTQGSoFUcV",
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:6e8c116d19d5c"
};
firebase.initializeApp(firebaseConfig);
var storage = firebase.storage();
var arg = process.argv.slice(2);
if (arg.length < 3) {
console.log("Usage: node createuser|download <email> <password>")
process.exit(1)
}
email = arg[1];
password = arg[2]
switch (arg[0]) {
case 'createuser':
firebase.auth().createUserWithEmailAndPassword(email,password).then(result => {
console.log("user created\n", result.user.email)
}).catch(err => {
console.log(err);
process.exit(1);
})
break;
case 'download':
if (arg.length < 4) {
console.log("Usage: node download <email> <password> <objectname>")
process.exit(1)
}
objectName = arg[3]
firebase.auth().signInWithEmailAndPassword(email, password).then(result => {
console.log("logged in as: " + result.user.email);
firebase.auth().currentUser.getIdToken(true).then(function(idToken) {
console.log(idToken);
var storageRef = storage.ref();
var forestRef = storageRef.child(objectName);
forestRef.getDownloadURL().then(url => {
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
console.log(data);
process.exit(0);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
process.exit(1);
});
}).catch(function(error) {
console.log(error);
process.exit(1);
});
}).catch(function(error) {
console.log("Unable to get ID token for current session:");
process.exit(1);
});
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage);
process.exit(1);
});
break;
default:
console.log('Sorry, argument must be either createuser or download');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment