Skip to content

Instantly share code, notes, and snippets.

View marshallswain's full-sized avatar
😄

Marshall Thompson marshallswain

😄
View GitHub Profile
@marshallswain
marshallswain / states.js
Created March 18, 2018 02:01
US States & Abbreviations as JavaScript Array
var usStates = [
{ name: 'ALABAMA', abbreviation: 'AL'},
{ name: 'ALASKA', abbreviation: 'AK'},
{ name: 'AMERICAN SAMOA', abbreviation: 'AS'},
{ name: 'ARIZONA', abbreviation: 'AZ'},
{ name: 'ARKANSAS', abbreviation: 'AR'},
{ name: 'CALIFORNIA', abbreviation: 'CA'},
{ name: 'COLORADO', abbreviation: 'CO'},
{ name: 'CONNECTICUT', abbreviation: 'CT'},
{ name: 'DELAWARE', abbreviation: 'DE'},
@marshallswain
marshallswain / auth-check-access-token.ts
Last active August 5, 2023 19:41
Example FeathersJS auth code. I have not tested it with even a single request. I just didn't want to lose it.
import type { HookContext, NextFunction } from '@feathersjs/feathers'
type GetTokenFn = <H extends HookContext>(context: H) => Promise<string>
interface CheckAccessTokenHookOptions {
getToken?: GetTokenFn | GetTokenFn[]
}
const defaultGetTokenFn: GetTokenFn = async context => context.params.headers?.authorization?.split(' ')[1]
-- See Nauany's full blog post at https://blog.bitovi.com
WITHRECURSIVEx(i)
AS (
VALUES(0)
UNIONALLSELECT i + 1FROM xWHERE i < 101
),
Z(Ix, Iy, Cx, Cy, X, Y, I)
AS (
SELECT Ix, Iy, X::float, Y::float, X::float, Y::float, 0
FROM(SELECT -2.2 + 0.031 * i, iFROM x)AS xgen(x,ix)
@marshallswain
marshallswain / hook.map-create-to-upsert.js
Last active June 16, 2022 18:29
A Feathers hook to remap service.create to do an upsert
module.exports = function (upsertQuery) {
if (typeof upsertQuery !== 'function') {
console.warn('No `upsertQuery` function was passed to the mapCreateToUpsert hook. Please set params.upsertQuery in the hook context to dynamically declare the function.')
}
return function mapCreateToUpsert (context) {
const { service, data, params } = context // const data = { address: '123', identifier: 'my-identifier' }
upsertQuery = params.upsertQuery || upsertQuery
if (typeof upsertQuery !== 'function') {
@marshallswain
marshallswain / example-usage.js
Last active May 17, 2022 00:34
A FeathersJS hook to implement `findOrCreate`
const findOrCreate = require('./hook.find-or-create.js')
app.service('messages').hooks({
before: {
create: [findOrCreate()]
}
})
@marshallswain
marshallswain / feathers-mongoose-upsert-2.js
Last active November 24, 2021 10:18
Another example of feathers-mongoose upserting
const data = { address: '2', identifier: 'some-other-identifier' }
const params = {
query: { address: '2' },
mongoose: { upsert: true }
}
app.service('address-meta').patch(null, data, params)
@marshallswain
marshallswain / readme.md
Created April 17, 2017 12:40
FeathersJS: Better permissions

Better Permissions Control

We have introduced 3 new hooks and 2 new middleware as part of feathers-permissions that give you much more flexibility and control over access permissions than was previously possible. Permissions are stored in the database on the entity record that needs to have access permissions checked (typically a user). They look like this:

[
    '*', // all services, all methods, all docs
    'users:*', // all methods on users service
    'users:remove:*', // can remove any user
    '*:remove', // can remove on any service
@marshallswain
marshallswain / authentication.js
Last active September 24, 2021 08:59
Example tools for using querystring redirects with Feathers OAuth login.
'use strict';
const authentication = require('feathers-authentication');
const jwt = require('feathers-authentication-jwt');
const local = require('feathers-authentication-local');
const oauth2 = require('feathers-authentication-oauth2');
const GithubStrategy = require('passport-github');
// Bring in the oauth-handler
const makeHandler = require('./oauth-handler');
@marshallswain
marshallswain / app.js
Last active May 18, 2021 17:08
Authenticate on feathers-socketio connection with header
const socketio = require('feathers-socketio')
const authOnSocketConnect = require('./authenticate-on-socket-connect')
// ... Setup your Feathers app code or use the generator then replace the socketio registration with this
// When you register the feathers-socketio plugin, use the utility
app.configure(socketio(function (io) {
// Get Socket.io headers
io.on('connection', function (socket) {
authOnSocketConnect({ app, socket })
@marshallswain
marshallswain / express-headers.json
Last active May 5, 2020 05:11
Feathers Client request headers using Socket.io, Express and Primus
{
"accept": "application/json, text/javascript, */*; q=0.01",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.8,cy;q=0.6",
"authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6ImFjY2VzcyJ9.eyJ1c2VySWQiOiI0akZ4SEtQWXRpcnVwMkROIiwiaWF0IjoxNDkxNTM3ODExLCJleHAiOjE0OTE2MjQyMTEsImF1ZCI6Imh0dHBzOi8veW91cmRvbWFpbi5jb20iLCJpc3MiOiJmZWF0aGVycyIsInN1YiI6ImFub255bW91cyJ9.zOqO6bUgQrsMy7JIea6eoDCrMUqnIj2qKE8CPFOvhsQ",
"cache-control": "no-cache",
"connection": "keep-alive",
"content-length": "31",
"content-type": "application/json",
"host": "localhost:3030",