Skip to content

Instantly share code, notes, and snippets.

View deltaepsilon's full-sized avatar
🥰
Moving to private repos!!!

Chris Esplin deltaepsilon

🥰
Moving to private repos!!!
View GitHub Profile
@deltaepsilon
deltaepsilon / authorization-on-create.js
Last active July 23, 2018 12:57
Cloud Function to manage Custom Claims
const getCustomClaimsByEmail = require('../utilities/get-custom-claims-by-email');
const setCustomClaims = require('../utilities/set-custom-claims');
module.exports = ({ admin, environment }) => user => {
const db = admin.firestore();
const usersCollection = db.collection(environment.schema.users);
const customClaimsRef = admin.database().ref(environment.schema.customClaims);
const auth = admin.auth();
const email = extractEmailFromUser(user);

Keybase proof

I hereby claim:

  • I am deltaepsilon on github.
  • I am chrisesplin (https://keybase.io/chrisesplin) on keybase.
  • I have a public key ASDNf4HlYGnWXIBBydCSgZa1Wk1q2Z7eUr0Xa5x62ffqbQo

To claim this, I am signing this object:

@deltaepsilon
deltaepsilon / firebase.mixin.js
Created July 28, 2017 10:38
A Polymer 2.0 mixin to watch a bunch of Firebase refs. Returns an observable.
if (typeof window.FirebaseMixin == 'undefined') {
window.FirebaseMixin = superclass => {
if (!superclass) {
superclass = class Superclass {};
}
return class extends superclass {
constructor() {
super();
this.__firebaseMixinInit();
// Good
exports.sendEmail = functions.auth.user().onCreate(event => {
const mailgun = require('mailgun-js');
// ...
});
// Not As Awesome
const mailgun = require('mailgun-js');
exports.sendEmail = functions.auth.user().onCreate(event => {
const function = require('firebase-functions');
functions.database.ref('queues/login/{uid}').onWrite(event => {
const config = functions.config();
// functions.config() returns your environment variables.
// In this case I have an array of my admin users' email addresses in accessControlLists.adminUsers
// It looks like ['[email protected]', '[email protected]']
const adminUsersString = config.['access-control-lists']['admin-users'];
const adminUsers = adminUsersString.split(',');
const user = event.data.val();
// Client-side code, in my case a web browser
firebase.auth().onAuthStateChanged(function (user) {
this.user = user; // Set my local copy of the 'user' object
if (user) {
this.displayName = user.displayName;
this.photoURL = user.photoURL;
var userRef = firebase.database().ref('queues/login').child(user.uid);
userRef.remove()
.then(function () {
functions.auth.user().onCreate(event => {
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const config = functions.config();
admin.initializeApp(config.firebase);
const user = event.data;
const userRef = admin.database().ref('/users').child(user.uid);
exports.sendWelcomeEmail = functions.auth.user().onCreate(event => {
const user = event.data;
const email = user.email;
// …
});
exports.sendByeEmail = functions.auth.user().onDelete(event => {
const user = event.data;
const email = user.email;
// …
});
{
"database": {
"rules": "database.rules.bolt"
},
"hosting": {
"public": "dist",
"rewrites": [
{
"source": "/app/**/*",
"destination": "/index.html"
{
"rules": {
"userOwned": {
"preferences": {
"kanyesUID": {
"loveKanye": true
},
"taylorsUID": {
"listenToHaters": false
}