Skip to content

Instantly share code, notes, and snippets.

{
"albums": {
"key1": {
"owner": "uid1",
"subscribers": {
"uid1": true,
"uid2": true
}
}
}
@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 / bad.js
Last active March 6, 2019 03:48
Discuss using mocks in Firebase unit tests and where this falls down.
/**
Consider this data structure, used for getting a list of names based on membership
/users/<user id>/name
/rooms/members/<user id>/true
Now let's create a couple simple classes without any real consideration for testing structures, assuming
we'll use a mock of Firebase to test them. Note that I see these sorts of errors constantly in the wild;
this example is not far fetched or exaggerated (it's rather tame in comparison)
*/
@katowulf
katowulf / encode_firebase_keys.js
Created January 12, 2017 16:39
Encode Firebase keys to escape any restricted meta characters
function encodeKeys(data) {
let encodedData = null;
if( typeof(data) === 'function' ) {
encodedData = data + '';
}
else if (typeof(data) !== 'object') {
encodedData = data;
}
else if(data !== null) {
@katowulf
katowulf / app.detail.ts
Created November 8, 2016 16:34
AngularFire2 master detail example, uses routing with resolve method, Angular 2 + Firebase
import { Component } from '@angular/core';
import { AngularFire, FirebaseObjectObservable } from 'angularfire2';
import {ActivatedRoute, Params} from "@angular/router";
import {Observer} from "rxjs";
@Component({
selector: 'app-detail',
template: `
<h2>{{book.title}}</h2>
<p>{{book.author}}</p>
@katowulf
katowulf / firebase.json
Created July 11, 2016 19:37
Sample deploy config for Angular 2 routing with Firebase as found here https://github.com/katowulf/ngphx2016
{
"hosting": {
"public": "dist",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
@katowulf
katowulf / auth.js
Created June 30, 2016 22:07
Auth with Firebase 3.x and Cordova/Ionic using ngCordova oauth.
this.$cordovaOauth.facebook("your_client_id", ["email"])
.then(function (result) {
var credentials = firebase.auth.FacebookAuthProvider.credential(result.access_token);
return firebase.auth().signInWithCredential(credentials);
})
.then(function (firebaseUser) {
console.log("Signed in as:", firebaseUser.uid);
})
.catch(function (error) {
console.error("Authentication failed:", error);
@katowulf
katowulf / descending_order_firebasearray.js
Created June 10, 2016 18:25
Descending sorted array in AngularFire, accomplished by unshift() instead of push().
app.factory('DescendingArray', function($firebaseArray) {
return $firebaseArray.$extend({
// Modify $$process to handle added events by prepending instead of appending
$$process: function(event, rec, prevChild) {
var key = this.$$getKey(rec);
var changed = false;
if( event === 'child_added' ) {
curPos = this.$indexFor(key);
@katowulf
katowulf / oldnew.js
Last active June 3, 2016 01:50
Include old/new values in $firebaseArray::$watch method.
angular.factory('Arr', function($firebaseArray) {
function Arr(ref) {
this.oldVals = {};
return $firebaseArray.call(this, ref);
}
Arr.prototype.$$process = function(event, rec, prevKey) {
$firebaseArray.prototype.$$process.apply(this, arguments);
this.oldVals[rec.$id] = rec;
};
@katowulf
katowulf / rules.js
Created March 3, 2016 00:05
Search for users by email address in Firebase, without exposing everyone's email address to the world in a bulk-readable format.
{
"rules": {
"users": {
"$user_id": {
// email address is required
".validate": "newData.hasChildren(['email'])",
}
},
"emails_to_users": {