Skip to content

Instantly share code, notes, and snippets.

View thebigredgeek's full-sized avatar
💭
Considering new contract opportunities at this time

Andrew E. Rhyne thebigredgeek

💭
Considering new contract opportunities at this time
View GitHub Profile
@thebigredgeek
thebigredgeek / express_with_jwt.js
Last active November 5, 2023 18:35
Express API with JWT
var express = require('express')
, jwtMiddleware = require('express-jwt')
, bodyParser = require('body-parser')
, cookieParser = require('cookie-parser')
, cors = require('cors');
// We pass a secret token into the NodeJS process via an environment variable.
// We will use this token to sign cookies and JWTs
var SECRET_TOKEN = process.env.SECRET_TOKEN;
import express, { Request } from 'express';
import Context from './context'
const app = express();
// ...
app.get('/foo', (req: Request, res) => {
const ctx: Context = Context.get(req);
@thebigredgeek
thebigredgeek / middleware.ts
Created September 9, 2020 18:35
Context Middleware for Express
import express, { Request } from 'express';
import Context from './context'
const app = express();
// Middleware
app.use((req: Request, res: any, next: any) => {
Context.bind(req);
next();
});
@thebigredgeek
thebigredgeek / contextclass.ts
Last active September 9, 2020 18:33
Context Class for Requests
import { Request } from 'express';
export default class Context {
static _bindings = new WeakMap<Request, Context>();
public foo = 'bar';
constructor () {}
static bind (req: Request) : void {
@thebigredgeek
thebigredgeek / middleware.js
Created September 9, 2020 18:09
Express req decoration
// ...
app.use((req, res, next) => {
req.foo = 'hello world';
next();
});
@thebigredgeek
thebigredgeek / example.js
Created May 19, 2020 20:16
JWT Tenant Secret Middleware
const model = require('./model'); // our fake model
const verifyJWT = async (req, res, next) => {
// Extract the authorization header
const header = req.get('Authorization');
let err
, secret
, id;
@thebigredgeek
thebigredgeek / login.js
Last active May 19, 2020 19:48
JWT Login Example
const express = require('express')
, { json } = require('body-parser')
, jwt = require('jsonwebtoken')
, to = require('await-to-js')
, model = require('./model'); // fake model
// This only lives on the server, never the client!
const JWT_SECRET = process.env.JWT_SECRET;
// create a server instance
@thebigredgeek
thebigredgeek / endpoint.js
Last active May 18, 2020 18:52
JWT Protected Endpoint
const verifyJWT = async (req, res, next) => {
// Extract the authorization header
const header = req.get('Authorization');
// If no authorization header is present,
// send back an error
if (!header) {
res.status(401).send({
message: "Authorization required"
});
@thebigredgeek
thebigredgeek / netlify.log
Created March 24, 2020 21:08
netlify failed deploy
3:57:02 PM: Build ready to start
3:57:04 PM: build-image version: 2dbd444fcdce00cf06325060a8238d5ae3e86774
3:57:04 PM: build-image tag: v3.3.7
3:57:04 PM: buildbot version: 11918e084194721d200458438c92ff8180b3b56c
3:57:05 PM: Fetching cached dependencies
3:57:05 PM: Starting to download cache of 254.9KB
3:57:05 PM: Finished downloading cache in 164.787524ms
3:57:05 PM: Starting to extract cache
3:57:05 PM: Failed to fetch cache, continuing with build
3:57:05 PM: Starting to prepare the repo for build
@thebigredgeek
thebigredgeek / netlify.log
Created March 24, 2020 21:08
netlify failed deploy
3:57:02 PM: Build ready to start
3:57:04 PM: build-image version: 2dbd444fcdce00cf06325060a8238d5ae3e86774
3:57:04 PM: build-image tag: v3.3.7
3:57:04 PM: buildbot version: 11918e084194721d200458438c92ff8180b3b56c
3:57:05 PM: Fetching cached dependencies
3:57:05 PM: Starting to download cache of 254.9KB
3:57:05 PM: Finished downloading cache in 164.787524ms
3:57:05 PM: Starting to extract cache
3:57:05 PM: Failed to fetch cache, continuing with build
3:57:05 PM: Starting to prepare the repo for build