Skip to content

Instantly share code, notes, and snippets.

@katowulf
katowulf / logger.ts
Created May 9, 2019 04:59
A simple JavaScript/TypeScript logger for outputting messages to console and to DOM
'use strict';
///////////////////////////////////
// Rudimentary logger impl that outputs
// to the JS console and to <div> tag
// in demo app.
//
// ♡ Firebase
///////////////////////////////////
@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 / 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 / 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 / rules.js
Created November 16, 2018 19:20
Group inheritance and doc sharing in Firebase rules. See https://roles-example-rtdb.stackblitz.io/
{
"rules": {
".read": false,
".write": false,
"roles-example": {
// grant global read to our admin tools
".read": "auth.uid === 'ACCESS_MANAGER'",
// The documents which we are restricting access to
"docs": {
@katowulf
katowulf / call.js
Last active June 4, 2019 20:09
Modular Functions layout for Firebase
export const run = (data, context) => {
const path = data.path;
if( path !== "bar" ) {
throw new functions.https.HttpsError('invalid-argument', "Path was not valid");
}
return {foo: "bar"};
}
@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 / functions_index.js
Last active January 31, 2020 07:38
Upload file to Storage, add custom metadata, and fetch that metadata in Functions
const functions = require('firebase-functions');
const util = require('util');
const admin = require('firebase-admin');
admin.initializeApp();
exports.uploadedFile = functions.storage.object().onFinalize((uploadedObject) => {
console.log('object keys', Object.keys(uploadedObject));
// This contains the custom metadata
console.log('metedata keys', Object.keys(uploadedObject.metadata));
@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 / 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) {...}