Skip to content

Instantly share code, notes, and snippets.

View itsjavi's full-sized avatar

Javi Aguilar itsjavi

View GitHub Profile
@itsjavi
itsjavi / lucia-nextjs-csrf-route-guard.ts
Created March 29, 2024 02:54
CSRF protection guard function for API routes in Next.js App Router, with Lucia Auth
import { verifyRequestOrigin } from 'lucia'
import { NextResponse } from 'next/server'
type RouteHandler = (request: Request) => NextResponse | Response
// Wrap your Next.js Route Handler with this middleware to protect against CSRF attacks
// NOTE: this won't protect against CSRF attacks on Server Actions and Server Components,
// so it's better to configure this in middleware.ts
export function csrfGuard(guardedFn: RouteHandler): RouteHandler {
return (request: Request, ...context) => {
@itsjavi
itsjavi / next-dev--open.sh
Last active November 25, 2023 03:29
Open nextjs dev server URL after start
#!/bin/bash
set -e
# Load .env file
if [ -f .env ]; then
export $(cat .env | sed 's/#.*//g' | xargs)
else
echo "No .env file found"
exit 1
@itsjavi
itsjavi / components__ServiceWorkers.tsx
Last active March 11, 2024 15:02
Simple Service Worker for Next.js static assets
'use client'
import { useEffect } from 'react'
export function ServiceWorkers(): JSX.Element {
useEffect(() => {
if (typeof window === 'undefined') {
return
}
if ('serviceWorker' in navigator) {
@itsjavi
itsjavi / .zprofile
Last active March 28, 2023 12:44
Switch node version in the current zsh session, depending on an env var
export N_SESSION_NODE_VERSION=18
# Then, in your PHPStorm or Webstorm project settings you can configure the
# Terminal to override this value via N_SESSION_NODE_VERSION_OVERRIDE, e.g.:
# N_SESSION_NODE_VERSION_OVERRIDE=16
#
@itsjavi
itsjavi / polymorphic.tsx
Created March 20, 2023 09:59
Polymorphic component props
import React from 'react'
type PolymorphicPropsFactory<T, P> = {
as?: T | React.ElementType
children?: React.ReactNode | undefined
className?: string | undefined
} & React.RefAttributes<T> &
P
export type PolymorphicProps<T, FallbackPropsType> =
@itsjavi
itsjavi / polymorphic-btn.tsx
Last active January 27, 2023 14:51
TypeScript Polymorphic React Button Component using the "as" property
import React from 'react'
type PolymorphicPropsFactory<T, P> = {
as?: T | React.ElementType
children?: React.ReactNode | undefined
} & React.RefAttributes<T> &
P
type PolymorphicProps<T, FallbackPropsType> =
// as = Function Component
@itsjavi
itsjavi / SaveChatGPT-bookmark.js
Last active January 26, 2023 06:02
Bookmark to download ChatGPT chat as Markdown
// Copy the following line of code, and paste it prefixed with "javascript:" (without the quotes), in a browser bookmark
function h(html) { return html.replace(/<p>/g, '\n\n').replace(/<\/p>/g, '').replace(/<b>/g, '**').replace(/<\/b>/g, '**').replace(/<i>/g, '_').replace(/<\/i>/g, '_').replace(/<code[^>]*>/g, (match) => { const lm = match.match(/class="[^"]*language-([^"]*)"/); return lm ? %27\n```%27 + lm[1] + %27\n%27 : %27```%27; }).replace(/<\/code[^>]*>/g, %27```%27).replace(/<[^>]*>/g, %27%27).replace(/Copy code/g, %27%27).replace(/This content may violate our content policy. If you believe this to be in error, please submit your feedback — your input will aid our research in this area./g, %27%27).trim(); } (()=>{ const e=document.querySelectorAll(".text-base");let t="";for(const s of e)s.querySelector(".whitespace-pre-wrap")&&(t+=`**${s.querySelector(%27img%27)?%27You%27:%27ChatGPT%27}**: \n${h(s.querySelector(".whitespace-pre-wrap").innerHTML)}\n\n`);const o=document.createElement("a");o.download=wi
@itsjavi
itsjavi / CSS-in-JS-noSSR.tsx
Last active January 26, 2023 06:03
Simple CSS-in-JS implementation for React, using tagged templates, with scoped class name generation. Zero dependencies.
import React, { useEffect } from 'react'
type StyledFC = React.FC<{ children?: React.ReactNode }>
type StyledModule = <T extends string[]>(
...classNames: T
) => {
Styled: StyledFC
classMap: Record<T[number], string>
}
https://docs.google.com/presentation/d/1oqR-9VwK1fSFriMKneYnr2RUTo-jTmsbmwOi1ayrMp0/edit?usp=sharing
@itsjavi
itsjavi / delete_all_repos.sh
Last active August 30, 2022 08:14
Clone and delete all your gitlab repos
for repo in $(curl -s --header "PRIVATE-TOKEN: YOUR_PERSONAL_ACCESS_TOKEN" "https://gitlab.com/api/v4/projects/?simple=yes&private=true&owned=true&per_page=1000" | jq -r ".[].id");
do curl -H "Content-Type: application/json" --header "PRIVATE-TOKEN: YOUR_PERSONAL_ACCESS_TOKEN" -X DELETE "https://gitlab.com/api/v4/projects/$repo";
done;