Skip to content

Instantly share code, notes, and snippets.

View bcnzer's full-sized avatar

Ben Chartrand bcnzer

View GitHub Profile
@bcnzer
bcnzer / postman-pre-request.js
Last active April 23, 2024 19:26
Postman pre-request script to automatically get a bearer token from Auth0 and save it for reuse
const echoPostRequest = {
url: 'https://<my url>.auth0.com/oauth/token',
method: 'POST',
header: 'Content-Type:application/json',
body: {
mode: 'application/json',
raw: JSON.stringify(
{
client_id:'<your client ID>',
client_secret:'<your client secret>',
@bcnzer
bcnzer / cloudflareworker-verifyjwt.js
Last active March 21, 2024 14:09
Sample Cloudflare worker that gets the JWT, ensures it hasn't expired, decrypts it and returns a result
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
// Following code is a modified version of that found at https://blog.cloudflare.com/dronedeploy-and-cloudflare-workers/
/**
* Fetch and log a request
* @param {Request} request
*/
@bcnzer
bcnzer / worker.js
Created October 15, 2018 09:31
Cloudflare Worker that uses Workers KV to get Authorization data. All authentication and authorization is done on the edge
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
/**
* Entry point of the worker
*/
async function handleRequest(event) {
try {
// Get the JWT
@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 / 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 / web.config
Last active January 2, 2023 16:28
Web.config for use with a Vue.js SPA (single page app) and woff font files
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension="woff" mimeType="application/font-woff" />
<mimeMap fileExtension="woff2" mimeType="application/font-woff" />
</staticContent>
<rewrite>
<rules>
<rule name="Handle History Mode and custom 404/500" stopProcessing="true">
@bcnzer
bcnzer / local.settings.json
Created September 10, 2018 10:31
Example local.settings.json file for an Azure Functions project which specifies the CORS setting locally, for debug/dev
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"ConnectionStrings": {
"MyDb": "<my connection string>"
},
@bcnzer
bcnzer / SetCsProjVersion.ps1
Last active November 3, 2022 22:01
Powershell script for generating a version number and inserting it into the csproj
$csprojPath = $args[0] # path to the file i.e. 'C:\Users\ben\Code\csproj powershell\MySmallLibrary.csproj'
$newVersion = $args[1] # the build version, from VSTS build i.e. "1.1.20170323.1"
Write-Host "Starting process of generating new version number for the csproj"
$splitNumber = $newVersion.Split(".")
if( $splitNumber.Count -eq 4 )
{
$majorNumber = $splitNumber[0]
$minorNumber = $splitNumber[1]
@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 / 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) {