Skip to content

Instantly share code, notes, and snippets.

Ben Chartrand bcnzer

Block or report user

Report or block bcnzer

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bcnzer
bcnzer / quiz.html
Created May 26, 2020
Simple HTML and JS quiz
View quiz.html
<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 Feb 23, 2020
Example of how to perform a get, add and update using Cloud Firestore in a Function
View function.js
// 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 Feb 9, 2020
Example of using the getDownloadURL method to check for a file's existance
View getDownloadURL-example.js
// 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 Dec 27, 2019
Google Cloud Function for sending an email via SendGrid, triggered by a document being added to Firestore
View index.js
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 Dec 25, 2019
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
View launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "attach",
"name": "attach to Chrome",
"port": 9222,
"webRoot": "${workspaceFolder}",
"sourceMapPathOverrides": {
@bcnzer
bcnzer / nuxt.config.js
Created Dec 25, 2019
Partial nuxt.config.js file showing the extend method you need to add
View nuxt.config.js
/*
** 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 Dec 25, 2019
VS Code launch.json to enable debugging of Nuxt.js apps. Note a further change is required in your nuxt.config.js
View launch.json
{
"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 Apr 18, 2019
Example of a Cloudflare Worker and Worker KV storage used to transfer/migrate traffic to alternatively infrastructure
View worker.js
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 Nov 6, 2018
Example of a Cloudflare Worker handling Google reCAPTCHA in an "edged out" POST request
View workerPart4.js
// 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 Dec 12, 2019
Cloudflare Worker example of how you can handle the OPTIONS verb and set some CORS details in the response header
View workerOptions.js
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())
You can’t perform that action at this time.