Skip to content

Instantly share code, notes, and snippets.

@katowulf
katowulf / gist:4741111
Last active April 11, 2024 12:07
Firebase security rules for a simple chat room model
{
"chat": {
// the list of chats may not be listed (no .read permissions here)
// a chat conversation
"$key": {
// if the chat hasn't been created yet, we allow read so there is a way
// to check this and create it; if it already exists, then authenticated
// user (specified by auth.id) must be in $key/users
@katowulf
katowulf / firebase.json
Last active March 1, 2024 21:32
Example of Firebase emulator unit tests and seed Firestore data
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"emulators": {
"firestore": {
"port": 8080
}
}
@katowulf
katowulf / FirebaseMonitor.js
Last active January 6, 2024 04:19
Listen for Firebase events and survive authentication logouts and restarts.
/**
* This little helper is called instead of on() to monitor data and handle auth expiration
* It assumes that you monitor authentication state and re-auth if
* your auth token expires. It also doesn't help with any transactions, set ops, or
* other writes which could be in progress when authentication is lost--those will
* explode in glorious flashes of failure
*
* To support IE8, you will want these polyfills:
* forEach: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Polyfill
* indexOf: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill
@katowulf
katowulf / firebase_promise_wrapper.js
Last active January 6, 2024 04:19
Example of promise contracts for Firebase (using jQuery.Deferred)
/*
* Promise wrapper for Firebase
*
* Requires jQuery and underscore.js
*************************************/
(function ($) {
"use strict";
var undefined;
var FIREBASE_URL = 'https://YOURINSTANCE.firebaseio.com';
var Firebase = require("./firebase-node.js");
function Queue(ref) {
this._ref = ref;
}
Queue.prototype.pop = function(cb) {
this._ref.startAt().limit(1).once("child_added", this._pop.bind(this, cb));
}
@katowulf
katowulf / 1_query_timestamp.js
Last active September 21, 2023 20:28
Get only new items from Firebase
// assumes you add a timestamp field to each record (see Firebase.ServerValue.TIMESTAMP)
// pros: fast and done server-side (less bandwidth, faster response), simple
// cons: a few bytes on each record for the timestamp
var ref = new Firebase(...);
ref.orderByChild('timestamp').startAt(Date.now()).on('child_added', function(snapshot) {
console.log('new record', snap.key());
});
@katowulf
katowulf / enable_debug_logging.java
Last active August 18, 2023 03:15
Enable debug logging in Firestore in Java, Javascript, or Swift.
/******* Android **********/
// See https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/FirebaseFirestore.html#setLoggingEnabled(boolean)
FirebaseFirestore.setLoggingEnabled(true);
/******* Server-side Java **********/
/**
See https://medium.com/@hiranya911/logging-in-java-libraries-for-firebase-and-google-cloud-platform-f8742493b73f
1) Add the slf4j-simple binding to the application classpath
2) Set the -Dorg.slf4j.simpleLogger.defaultLogLevel=debug system property
**/
@katowulf
katowulf / 1_using_queries.js
Last active April 24, 2023 07:14
Methods to search for user accounts by email address in Firebase
/***************************************************
* Simple and elegant, no code complexity
* Disadvantages: Requires warming all data into server memory (could take a long time for MBs of data or millions of records)
* (This disadvantage should go away as we add optimizations to the core product)
***************************************************/
var fb = firebase.database.ref();
/**
* @param {string} emailAddress
@katowulf
katowulf / client.js
Last active April 10, 2023 06:35
Firestore security rules validation example
import * as firebase from "firebase/app";
import 'firebase/firestore';
import config from './config';
firebase.initializeApp(config);
const data = {
string: 'foo',
integer: 23,
boolean: false,
@katowulf
katowulf / simpleDocSharing_listMembers.js
Last active April 10, 2023 06:31
Psuedo rules examples for role based document sharing in Firestore.
// docs/{docId}/users is an array of user ids allowed to access the doc
match /docs/{docId} {
allow read if request.auth.uid in getData('docs/$(docId)').users;
}
/**
* Shortcut to simplify pathing
*/
function getPath(childPath) {