Skip to content

Instantly share code, notes, and snippets.

View balazsorban44's full-sized avatar
🌍
Coding from anywhere

Balázs Orbán balazsorban44

🌍
Coding from anywhere
View GitHub Profile
@balazsorban44
balazsorban44 / _middleware.js
Last active July 5, 2023 09:36
NextAuth.js Auth Middleware for Next.js 12
import { getToken } from "next-auth/jwt"
import { NextResponse } from "next/server"
export async function middleware(req) {
// return early if url isn't supposed to be protected
if (!req.url.includes("/protected-url")) {
return NextResponse.next()
}
const session = await getToken({ req, secret: process.env.SECRET })
@balazsorban44
balazsorban44 / client.ts
Created September 2, 2021 10:33
Simple GraphQL client
export default function GQLClient(params) {
return {
async request(query, variables) {
const token = params.token ?? (await getToken(params.client))
const res = await fetch(params.url, {
headers: {
"Content-Type": "application/json",
...(token ? { Authorization: `Bearer ${token}` } : undefined),
},
method: "POST",
@balazsorban44
balazsorban44 / logger.ts
Last active July 21, 2021 12:29
Next.js API route that parses minified stacks and sends it to third-party logger.
import { NextApiRequest, NextApiResponse } from "next"
import log, { LogLevel } from "utils/server-logger"
import StackTraceGPS from "stacktrace-gps"
import ErrorStackParser from "error-stack-parser"
import fs from "fs"
import path from "path"
export default async function logger(
req: NextApiRequest,
res: NextApiResponse
az aks get-credentials --resource-group k8s-demo-ss --name k8s-demo-cluster-ss --file kubeconfig-ss
import NextAuth from "next-auth"
import Providers from "next-auth/providers"
import { addSeconds } from "date-fns"
import type { User } from "hooks/useUser"
import log from "utils/server-logger"
import sessionsDB, { InactiveSessionReason } from "lib/session-db"
import jwtDecode from "jwt-decode"
/** @see https://docs.microsoft.com/en-us/azure/active-directory/develop/id-tokens#payload-claims */
export interface IDToken {
import NodeCache from "node-cache"
const cache = new NodeCache()
/**
* Retrieves an access_token from an OAuth Provider for building purposes,
* using client_credentials (client secret) authorization type.
*/
export default async function getBuildToken(){
try {
import fs from "fs"
import path from "path"
import chalk from "chalk"
const getDirectories = (source ) => {
const dirents = fs.readdirSync(source, { withFileTypes: true })
return dirents
.filter(
(dirent) =>
// I wish this file to be generated from my *.gql files
declare module '*/queries.gql' {
export const Something: string;
export const SomethingElse: string;
}
function profile(profile) {
return ({
...profile,
id: profile.sub,
name: profile["http://domain.com/Events/Customer/Fullname"] ?? profile["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"],
verified: profile["email_verified"] === "True",
})
}
@balazsorban44
balazsorban44 / useEventualScroll.ts
Created May 13, 2020 20:26
Scrolls to an element after that element is added to the DOM
import * as React from "react"
/** Scrolls to an element after that element is added to the DOM */
const useEventualScroll = (
/**
* Holds the element to scroll to.
* The closer it is to that element, the less Mutations are observed
* by MutationObserver. If not defined, document will be used.
*/
container?: null | HTMLElement