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
import { withAuth } from "next-auth/middleware";
export default withAuth(
function middleware(request) {
if (request.nextUrl.pathname !== "/") return;
return Response.redirect(new URL("/home", request.url));
},
// This is NextAuth.js
// Docs: https://next-auth.js.org/configuration/nextjs#pages
// Needs to be the same as your `authOptions.pages`
// app/api/auth/[...nextauth]/route.ts
import NextAuth, { type NextAuthOptions } from "next-auth"
import GitHub from "next-auth/providers/github"
export const authOptions: NextAuthOptions = {
providers: [
GitHub({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
@balazsorban44
balazsorban44 / api.ts
Last active December 15, 2022 16:12
Auth.js Example
import { AuthHandler } from "@auth/core"
// You get this from somewhere
const req = new Request("https://a.com/api/auth/session")
const res = await AuthHandler(req, {
// Make sure you trust the host header
trustHost: true,
// Eg.: `openssl rand -hex 32`
secret: "",
import NextAuth, { NextAuthOptions } from "next-auth"
import GithubProvider from "next-auth/providers/github"
export const authOptions: NextAuthOptions = {
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
],
// app/page.jsx
import { unstable_getServerSession } from "next-auth/next"
export default async function Page() {
const session = await unstable_getServerSession()
return <pre>{JSON.stringify(session, null, 2)}</pre>
}
//...
providers: [
Twitch({
clientId: process.env.TWITCH_ID,
clientSecret: process.env.TWITCH_SECRET,
authorization: { params: { scope: "openid user:read:email moderation:read" } },
async profile(profile, tokens) {
const id = profile.sub
const res = await fetch(`https://api.twitch.tv/helix/users?id=${id}}`, {
headers: { Authorization: `Bearer ${tokens.access_token}`, "Client-Id": process.env.TWITCH_ID },
@balazsorban44
balazsorban44 / index.html
Created March 4, 2022 02:03
Firefox bug: Image natural sizes reported incorrectly after the decode method has finished
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image width/height after decode</title>
<script>
window.onload = function() {
@balazsorban44
balazsorban44 / apple-gen-secret.mjs
Created November 27, 2021 13:08
Script to generate Apple Client secret
#!/bin/node
import { SignJWT } from "jose"
import { createPrivateKey } from "crypto"
if (process.argv.includes("--help") || process.argv.includes("-h")) {
console.log(`
Creates a JWT from the components found at Apple.
By default, the JWT has a 6 months expiry date.
Read more: https://developer.apple.com/documentation/sign_in_with_apple/generate_and_validate_tokens#3262048
const colors = {
primary: "bg-primary",
secondary: "bg-secondary"
} // 🥴 I don't want this...
export function Button(
props: {variant: "primary" | "secondary"}
) {
const { variant, ...rest } = props
return (
@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 })