Skip to content

Instantly share code, notes, and snippets.

@bcnzer
bcnzer / quiz.html
Created May 26, 2020 10:07
Simple HTML and JS quiz
<html>
<body>
<div class="question">
Q1: What is fun?
</div>
<div>
<input type="radio" id="a0" name="question0">
<label for="a0">Going for a walk</label>
</div>
<div>
@bcnzer
bcnzer / function.js
Created February 23, 2020 03:49
Example of how to perform a get, add and update using Cloud Firestore in a Function
// Make sure to use firebase-admin and to initialize the app
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
// Example of how I'm GETTING all the records in a collection AND using
// the ID from the path of the document that triggered this function
exports.getLessonsAsync = functions.firestore
.document('organizations/{organizationId}/lessons/{lessonId}')
.onWrite(async function(change, context) {
const result = await admin
@bcnzer
bcnzer / getDownloadURL-example.js
Created February 9, 2020 09:41
Example of using the getDownloadURL method to check for a file's existance
// Using promise
const listRef = storage
.ref('screenshots/abc123.png')
.getDownloadURL()
.then((response) => {
// Found it. Do whatever
})
.catch((err) => {
// Didn't exist... or some other error
})
@bcnzer
bcnzer / index.js
Created December 27, 2019 21:35
Google Cloud Function for sending an email via SendGrid, triggered by a document being added to Firestore
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
const SENDGRID_API_KEY = functions.config().sendgrid.key
const sendGridEmail = require('@sendgrid/mail')
sendGridEmail.setApiKey(SENDGRID_API_KEY)
@bcnzer
bcnzer / launch.json
Created December 25, 2019 09:03
VS Code debug config in which we are attaching to the Chrome instance. Note that you must launch Chrome with the remote debugging port i.e. chrome.exe --remove-debugging-port=9222
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "attach",
"name": "attach to Chrome",
"port": 9222,
"webRoot": "${workspaceFolder}",
"sourceMapPathOverrides": {
@bcnzer
bcnzer / nuxt.config.js
Created December 25, 2019 00:47
Partial nuxt.config.js file showing the extend method you need to add
/*
** Build configuration
*/
build: {
/*
** You can extend webpack config here
*/
extend(config, ctx) {
if (ctx.isDev) {
config.devtool = ctx.isClient ? 'source-map' : 'inline-source-map'
@bcnzer
bcnzer / launch.json
Created December 25, 2019 00:40
VS Code launch.json to enable debugging of Nuxt.js apps. Note a further change is required in your nuxt.config.js
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "client: chrome",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}",
"sourceMapPathOverrides": {
@bcnzer
bcnzer / worker.js
Last active September 28, 2022 22:26
Example of a Cloudflare Worker and Worker KV storage used to transfer/migrate traffic to alternatively infrastructure
addEventListener('fetch', event => {
event.passThroughOnException()
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const maxBizId = await MIGRATION_SETTINGS.get('maxbizid')
const currentBusinessId = getBusinessIdFromCookie(request)
if (!maxBizId || maxBizId == 0 || !currentBusinessId) {
@bcnzer
bcnzer / workerPart4.js
Last active September 28, 2022 22:26
Example of a Cloudflare Worker handling Google reCAPTCHA in an "edged out" POST request
// NOTE: all auth code has been removed for the sake of brevity. Please see part 2 of blog series
// for more info: https://liftcodeplay.com/2018/10/16/pushing-my-api-to-the-edge-part-2-authentication-and-authorization/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
const genderFemale = 'Female'
const genderMale = 'Male'
/**
@bcnzer
bcnzer / workerOptions.js
Last active December 12, 2019 09:19
Cloudflare Worker example of how you can handle the OPTIONS verb and set some CORS details in the response header
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
/**
* Entry point of the worker
*/
async function handleRequest(event) {
// Generate the CORS headers I'll have to return with requests
const corsHeaders = setCorsHeaders(new Headers())