Skip to content

Instantly share code, notes, and snippets.

@katowulf
katowulf / delete_users_throttled.js
Created March 6, 2018 16:28
Delete users in Firebase Authentication using Admin SDK
const admin = require('firebase-admin');
const serviceAccount = require('path/to/serviceAccountKey.json');
// User IDs to be deleted
const UIDs = [];
// initialize the app
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
@katowulf
katowulf / I Dream in Code.js
Last active January 26, 2023 17:01
I Dream in Code...
/*********************
** I Dream in Code **
*********************/
while( I .sleep() ) {
I.dream() in code;
I.dream() in algorithms;
I.dream() in subroutines;
Rewind.age(5) && I.dream.of(["missing pants", "driving hotwheels", "peeing in fountains"]) ); //hint: it’s my bed
@katowulf
katowulf / firebase_copy.js
Last active July 29, 2022 15:58
Move or copy a Firebase path to a new location
function copyFbRecord(oldRef, newRef) {
oldRef.once('value', function(snap) {
newRef.set( snap.value(), function(error) {
if( error && typeof(console) !== 'undefined' && console.error ) { console.error(error); }
});
});
}
@katowulf
katowulf / client-index.ts
Last active June 22, 2022 21:04
Example of validating Firestore writes using Cloud Functions endpoints
import './style.css';
import logger from './logger'; // see https://gist.github.com/katowulf/08cd54013ad75f2c4d6cc9961ec77db1
import {sendRequest} from './request';
const endpoint = 'https://us-central1-YOUR_PROJECT_ID_HERE.cloudfunctions.net/validateRequest';
const data = {
string: 'foo',
integer: 23,
boolean: false,
@katowulf
katowulf / app.js
Last active February 24, 2022 06:51
Simple paginate example in AngularFire with Firebase.util (http://firebase.github.io/firebase-util/)
var app = angular.module('app', ['firebase']);
app.controller('ctrl', function($scope, $pageArray) {
$scope.pageItems = $pageArray(ref, 'number');
});
app.factory('$pageArray', function($firebaseArray) {
return function(ref, field) {
// create a Paginate reference
var pageRef = new Firebase.util.Paginate(ref, field, {maxCacheSize: 250});
@katowulf
katowulf / create_user_functions.js
Created December 3, 2018 22:23
Create a Firebase user from an authenticated Cloud Functions HTTPS endpoint.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const express = require('express');
const cookieParser = require('cookie-parser')();
const cors = require('cors')({origin: true});
const app = express();
// See https://github.com/firebase/functions-samples/blob/Node-8/authorized-https-endpoint/functions/index.js
const validateFirebaseIdToken = require('./validateFirebaseIdToken');
@katowulf
katowulf / print_ip_and_headers.js
Last active January 12, 2022 05:51
Print IP address and headers in Cloud Functions
const functions = require('firebase-functions');
const util = require('util');
exports.helloWorld = functions.https.onRequest((req, res) => {
// For Firebase Hosting URIs, use req.headers['fastly-client-ip']
// For callable functions, use rawRequest
// Some users have better success with req.headers['x-appengine-user-ip']
const ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const headers = JSON.stringify(req.headers, null, 2);
const message = util.format("<pre>Hello world!\n\nYour IP address: %s\n\nRequest headers: %s</pre>", ipAddress, headers);
@katowulf
katowulf / pseudo_code.js
Last active June 16, 2021 19:29
Convert Cloud Storage json file to event stream and store output in Firestore
JSONStream = require('JSONStream');
es = require('event-stream');
fileStream = storage.bucket('your-bucket').file('your-JSON-file').createReadStream();
db = admin.firestore();
return new Promise( (resolve, reject) => {
batchPromises = [];
batchSize = 0;
batch = db.batch();
@katowulf
katowulf / app.component.ts
Last active February 3, 2021 15:09
Dynamically set page title based on active route in Angular 4
// This can probably be simplified somehow. Not sure why I need to add it in the component to init the service.
import { Component, OnInit } from '@angular/core';
import {TitleService} from "./@core/utils/title.service";
@Component({...})
export class AppComponent implements OnInit {
constructor(private titleService: TitleService) {...}
@katowulf
katowulf / max.js
Created December 8, 2020 19:50
In Firestore node.js, set the max value on a field similar to https://cloud.google.com/firestore/docs/reference/rest/v1/Write#FieldTransform
import firebase from "firebase/app";
import "firebase/firestore";
const randomNumber = Math.floor(Math.random() * 1000);
const docRef = firebase.firestore.doc("path/to/doc");
db.runTransaction(async function(t) {
const doc = await t.get(docRef);
if (doc.exists && doc.data().number < randomNumber) {
console.log("setting new max");