Skip to content

Instantly share code, notes, and snippets.

@thiagosouza
Last active August 2, 2018 18:02
Show Gist options
  • Save thiagosouza/d23e116024953a42c0a0e7db0ecbb4b3 to your computer and use it in GitHub Desktop.
Save thiagosouza/d23e116024953a42c0a0e7db0ecbb4b3 to your computer and use it in GitHub Desktop.
#node.js #firebase #googlecloud #snippets
var firebase = require('firebase');
var admin = require("firebase-admin");
// Fetch the service account key JSON file contents
var serviceAccount = require("/Users/thiagosouza/.ssh/friendlychat-890e0-firebase-adminsdk-683gc-9bbc14166f.json");
// https://console.firebase.google.com/project/friendlychat-890e0/settings/serviceaccounts/adminsdk
// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://friendlychat-890e0.firebaseio.com"
});
// As an admin, the app has access to read and write all data, regardless of Security Rules
var db = admin.database();
var ref = db.ref("messages");
ref.once("value", function(snapshot) {
console.log(snapshot.val());
});
let ref = change.after.ref;
let doc = change.after;
var data = change.after.data();
let params = context.params;
doc = db.doc(`/payments/${params.payment_id}`); //TODO REMOVER
ref = db.doc(`/payments/${params.payment_id}`).ref; //TODO REMOVER
doc.get().then(dataTHIAGO=>{data = dataTHIAGO.data(); //TODO REMOVER
const secureCompare = require('secure-compare');
// Maximum concurrent account deletions.
const MAX_CONCURRENT = 3;
/**
* When requested this Function will delete every user accounts that has been inactive for 30 days.
* The request needs to be authorized by passing a 'key' query parameter in the URL. This key must
* match a key set as an environment variable using `firebase functions:config:set cron.key="YOUR_KEY"`.
*/
exports.accountcleanup = functions.https.onRequest((req, res) => {
const key = req.query.key;
// Exit if the keys don't match.
if (!secureCompare(key, functions.config().cron.key)) {
console.log('The key provided in the request does not match the key set in the environment. Check that', key,
'matches the cron.key attribute in `firebase env:get`');
res.status(403).send('Security key does not match. Make sure your "key" URL query parameter matches the ' +
'cron.key environment variable.');
return null;
}
//https://github.com/firebase/functions-samples/blob/master/delete-unused-accounts-cron/functions/index.js
{
"name": "google-cloud-storage",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@google-cloud/datastore": "^1.0.2",
"@google-cloud/storage": "^1.1.1",
"firebase": "^4.1.2",
"firebase-admin": "^5.0.0",
"google-cloud": "^0.55.0"
}
}
var storage = require('@google-cloud/storage');
var fs = require('fs');
var bucketName = "friendlychat-890e0.appspot.com";
// authentication, if needed
// var gcs = storage({
// projectId: 'friendlychat-890e0',
// keyFilename: '/Users/thiagosouza/.ssh/FriendlyChat-89ba1a2db93d.json'
// });
// Instantiates a client
var gcs = storage();
// Lists files in the bucket
gcs
.bucket(bucketName)
.getFiles()
.then((results) => {
const files = results[0];
console.log('Files:');
files.forEach((file) => {
console.log(file.name);
});
})
.catch((err) => {
console.error('ERROR:', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment