Skip to content

Instantly share code, notes, and snippets.

@torresalmonte
torresalmonte / index.js
Created October 16, 2018 17:43
Firebase Cloud Function using Cloud Vision API for safe search filter
const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('@google-cloud/storage')();
const spawn = require('child-process-promise').spawn;
const path = require('path');
const os = require('os');
const fs = require('fs');
/**
* When an image is uploaded we check if it is flagged as Adult or Violence by the Cloud Vision
@torresalmonte
torresalmonte / index.js
Created December 17, 2018 16:59
firebase admin sdk send verification email
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault()
});
// taken from here: https://stackoverflow.com/a/41886023/4139896
exports.testSendVerificationEmail = functions.https.onRequest(async (req, res) => {
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const axios = require('axios');
admin.initializeApp();
async function adminAuthEmailRequest(request) {
var token = await admin.apps[0].INTERNAL.getToken();
var requestCopy = JSON.parse(JSON.stringify(request));
requestCopy.headers = requestCopy.headers || {};
var authHeader = 'Authorization';

Fetch All Files from Firebase Hosting

This script fetches all of the files from the currently deployed version of a Firebase Hosting site. You must be signed in via the Firebase CLI and have "Site Viewer" permission on the site in question to be able to properly run the script.

Running via NPX

npx https://gist.github.com/mbleigh/9c8680cf319ace2f506f57380da66e7d <site_name>
@torresalmonte
torresalmonte / README.md
Created November 19, 2019 18:03 — forked from puf/README.md

Firebase Hosting Deploy Single File

This utility script deploy a single local file to an existing Firebase Hosting site. Other files that are already deployed are left unmodified.

The difference with firebase deploy is that this script does not require you to have a local snapshot of all hosted files, you just need the one file that you want to add/update.

USE AT YOUR OWN RISK. NO WARRANTY IS PROVIDED.

@torresalmonte
torresalmonte / admin_storage.js
Last active June 6, 2021 06:09
Script to get the downloadURL via Admin SDK; if the Firebase metadata required doesn't exists, it adds it
const admin = require('firebase-admin');
const createUuid = require('uuid-v4');
// assumes the existance of the GOOGLE_APPLICATION_CREDENCIAL env variable
admin.initializeApp({
credential: admin.credential.applicationDefault(),
storageBucket: '<YOUR_STORAGE_BUCKET>.appspot.com'
});
// it will get the list of files under "images" and create a downloadUrl for each one
@torresalmonte
torresalmonte / index.js
Created November 27, 2019 18:11
Script to delete Storage rulesets using the "firebaserules" REST API
const admin = require('firebase-admin');
const {sleep} = require('sleep');
// asumes the existance of the GOOGLE_APPLICATION_CREDENCIAL env variable
const firebaseApp = admin.initializeApp();
async function getRulesets(projectId, firebaseApp, pageToken) {
pageToken = pageToken || '';
@torresalmonte
torresalmonte / get_id_token.js
Created February 10, 2020 21:17
Testing Firebase Auth REST API
const https = require('https');
const API_KEY = "YOUR_API_KEY";
const email = "YOUR_EMAIL@EXAMPLE.COM";
const pass = "YOUR_SECRET_PASSWORD";
const signInUrl = `https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${API_KEY}`
// login data; this will be sent on the POST request
// NOTE: the parameter 'returnSecureToken' must be true to get the idToken
const postData = {
@torresalmonte
torresalmonte / drop_storage_rules.js
Created March 19, 2020 22:47
Script to delete the excess of security rulesets
const admin = require('firebase-admin');
const {sleep} = require('sleep');
// asumes the existance of the GOOGLE_APPLICATION_CREDENCIAL env variable
const firebaseApp = admin.initializeApp();
async function getRulesets(projectId, firebaseApp, pageToken) {
pageToken = pageToken || '';
const admin = require('firebase-admin');
// expects the environment variable GOOGLE_APPLICATION_CREDENTIALS
const serviceAccount = require(process.env.GOOGLE_APPLICATION_CREDENTIALS);
const credential = admin.credential.cert(serviceAccount);
const PROJECT_ID = '<FIREBASE_PROJECT_ID>';
const app = admin.initializeApp({
credential: credential