Skip to content

Instantly share code, notes, and snippets.

@katowulf
katowulf / the_problem.js
Created September 18, 2014 15:44
Explain the unknown provider <-- a error that occurs in dist files created with ngMin
// ngmin can handle this fine; if not using ngmin, then
// we'd do ['$provide', function($provide) { ... }]
app.config(function($provide) {
// however, ngmin does not know how to preserve the injected dependencies here!
$provide.decorator('SomeFactory', function($delegate, $timeout) {
});
});
@katowulf
katowulf / app.component.ts
Last active October 16, 2024 04:59
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 / print_ip_and_headers.js
Last active September 13, 2024 03:12
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 / rules.json
Last active September 5, 2024 17:32
Validating client-only auction bids in Firebase using security rules and transactions.
{
"auctions": {
"$auction_id": {
"current_bid": {
".validate": "newData.hasChildren(['uid', 'amount'])",
"uid": {
".validate": "newData.val() === auth.uid"
},
"amount": {
".validate": "newData.isNumber() && newData.val() > data.val() && newData.val() === root.child('auctions/'+$auction_id+'/bids/'+auth.uid+'/amount').val()"
@katowulf
katowulf / simpleDocSharing_listMembers.js
Last active July 27, 2024 00:35
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) {
@katowulf
katowulf / gist:4741111
Last active July 13, 2024 20:23
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 / 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 / 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';