Skip to content

Instantly share code, notes, and snippets.

View kevcodez's full-sized avatar
🚀

Kevin Grüneberg kevcodez

🚀
View GitHub Profile
@kevcodez
kevcodez / lock-advanced.js
Last active August 28, 2022 15:34
Advanced Distributed Lock with Mongo
/**
* Create the MongoDB collection and an expiring index on a field named "expiresAt".
*
* db.createCollection('locks');
* db.locks.createIndex( { "expiresAt": 1 }, { expireAfterSeconds: 0 } )
**/
async function acquireLock(name, ttlSeconds) {
const collection = await db.collection('locks');
@kevcodez
kevcodez / lock.js
Last active August 28, 2022 15:31
Simple Distributed Lock with Mongo
async function acquireLock(name) {
// Open connection to database
const collection = await db.collection('locks');
try {
// Insert an entry to the database, using the _id field, which already has a unique key constraint
await collection.insertOne({ _id: name });
// No exception was thrown, so we successfully acquired a lock
return true;
@kevcodez
kevcodez / auth0-rule.js
Created June 27, 2022 10:03
Auth0 notify backend on social auth "registration"
async function (user, context, callback) {
const axios = require('axios');
const isSocial = context.connectionStrategy === context.connection;
// if it is the first login (hence the `signup`) and it is a social login
if (context.stats.loginsCount === 1 && isSocial) {
let loginMethod = '';
const connectionName = user.identities[0].provider.toLowerCase();
@kevcodez
kevcodez / hook.sql
Created April 30, 2022 15:18
Supabase Before insert hook
-- Create a new trigger
CREATE OR REPLACE FUNCTION public.notify_parqet_api_signup()
RETURNS TRIGGER AS $$
DECLARE
parqet_user_id varchar;
social_provider_id varchar;
BEGIN
-- Extract ID from social provider, can be null
social_provider_id := NEW.raw_user_meta_data->>'provider_id';
function doYourStuff() {
const yourString = "foobar"
const uniqueString = findUniqueString(yourString)
// continue with your logic with unique string
}
function findUniqueString(baseString) {
let uniqueString = baseString
// number of iterations should be super small, probably unnecessary to have a max threshold
@kevcodez
kevcodez / gist:512ab3b368b5d690d925d30e00d7fec6
Created March 17, 2021 07:35
register custom components in nuxt
import { join } from "path";
export default function () {
const { nuxt } = this
// Make sure components is enabled
if (!nuxt.options.components) {
throw new Error('please set `components: true` inside `nuxt.config` and ensure using `nuxt >= 2.13.0`')
}
@kevcodez
kevcodez / settings.json
Created January 9, 2021 21:10
Nuxt Config + Github Pages
{
...
"logo": {
"light": "/<repo-name>/logo-light.svg",
"dark": "/<repo-name>/logo-dark.svg"
},
...
}
@kevcodez
kevcodez / nuxt.config.js
Created January 9, 2021 21:09
Nuxt Config + Github Pages
export default theme({
// ...
router: {
base: '/<repo-name>/'
}
})
@kevcodez
kevcodez / docs.yml
Last active January 12, 2021 11:30
Github Pages + Nuxt Content Deploy
name: docs
# define the trigger
on: [push, pull_request]
jobs:
docs:
runs-on: ${{ matrix.os }}
env:
# We used "docs" as directory name for our documentation, we will be referencing this later
@kevcodez
kevcodez / gist:0fa6f561315e73c26748dc847e32f113
Created June 7, 2020 10:25
OAuth 2.0 PKCE Flow with AWS Cognito - Login with public client, like CLI
import {
Issuer,
generators,
Client,
TokenSet,
CallbackParamsType,
} from "openid-client";
const http = require('http');
const issuer = await Issuer.discover('https://cognito-idp.eu-central-1.amazonaws.com/eu-central-1_VqitD3cvk/.well-known/openid-configuration')