diff --git a/README.md b/README.md index 86b60a5..5b6acd8 100644 --- a/README.md +++ b/README.md @@ -78,17 +78,17 @@ const config: NextAuthPubkeyConfig = { baseUrl: process.env.NEXTAUTH_URL, secret: process.env.NEXTAUTH_SECRET, storage: { - async set({ k1, session }) { - // save pubkey auth session data based on k1 id + async set({ k1, data }) { + // save pubkey data based on k1 id }, async get({ k1 }) { - // lookup and return pubkey auth session data based on k1 id + // lookup and return pubkey data based on k1 id }, - async update({ k1, session }) { - // update pubkey auth session data based on k1 id + async update({ k1, data }) { + // update pubkey data based on k1 id }, async delete({ k1 }) { - // delete pubkey auth session data based on k1 id + // delete pubkey data based on k1 id }, }, generateQr, @@ -167,7 +167,7 @@ const config: NextAuthPubkeyConfig = { * @param {object} storage * * pubkey auth flows require that a callback be triggered - * part of the authentication flow. So, we require session storage to + * part of the authentication flow. So, storage is required to * persist some data and ensure it's available when the callback is triggered. * Data can be stored in a medium of your choice. * @@ -184,8 +184,8 @@ const config: NextAuthPubkeyConfig = { * The k1 is a unique key that's used to store the * data for later use. */ - async set({ k1, session }) { - // save pubkey auth session data based on k1 id + async set({ k1, data }) { + // save pubkey data based on k1 id }, /** @@ -196,7 +196,7 @@ const config: NextAuthPubkeyConfig = { * and return data previously stored under it. */ async get({ k1 }) { - // lookup and return pubkey auth session data based on k1 id + // lookup and return pubkey data based on k1 id }, /** @@ -207,10 +207,10 @@ const config: NextAuthPubkeyConfig = { * update data previously stored under it. * * @note the storage.update method should throw an error if - * an existing session is not already stored under the k1. + * an existing data is not already stored under the k1. */ - async update({ k1, session }) { - // update pubkey auth session data based on k1 id + async update({ k1, data }) { + // update pubkey data based on k1 id }, /** @@ -221,7 +221,7 @@ const config: NextAuthPubkeyConfig = { * delete data previously saved data. */ async delete({ k1 }) { - // delete pubkey auth session data based on k1 id + // delete pubkey data based on k1 id }, }, @@ -493,7 +493,7 @@ const config: NextAuthPubkeyConfig = { # Storage -pubkey auth flows require that a callback be triggered on success as part of the authentication flow. For this reason, it may be that the device scanning the QR (e.g. a mobile) is not the same device that's trying to authenticate (e.g. a desktop). So, we require session storage to persist some data and make it available across devices and ensure it's available when the callback is triggered. +pubkey auth flows require that a callback be triggered on success as part of the authentication flow. For this reason, it may be that the device scanning the QR (e.g. a mobile) is not the same device that's trying to authenticate (e.g. a desktop). So, we require data storage to persist some data and make it available across devices and ensure it's available when the callback is triggered. Data can be stored in a medium of your choice. For example: a database, a document store, or a session store. Here's an example using [Vercel KV](https://vercel.com/docs/storage/vercel-kv): @@ -503,15 +503,15 @@ import { kv } from "@vercel/kv"; const config: NextAuthPubkeyConfig = { // ... storage: { - async set({ k1, session }) { - await kv.set(`k1:${k1}`, session); + async set({ k1, data }) { + await kv.set(`k1:${k1}`, data); }, async get({ k1 }) { return await kv.get(`k1:${k1}`); }, - async update({ k1, session }) { + async update({ k1, data }) { const old = (await kv.get(`k1:${k1}`)) || {}; - await kv.set(`k1:${k1}`, { ...old, ...session }); + await kv.set(`k1:${k1}`, { ...old, ...data }); }, async delete({ k1 }) { await kv.del(`k1:${k1}`); @@ -529,7 +529,7 @@ Once you have configured the storage functions you can launch your dev server an http://localhost:3000/api/pubkey/diagnostics ``` -On the diagnostic page you can optionally pass in your own custom session values via query param: +On the diagnostic page you can optionally pass in your own custom values via query param: ``` http://localhost:3000/api/pubkey/diagnostics?k1=custom-k1&state=custom-state&pubkey=custom-pubkey&sig=custom-sig diff --git a/TODO.md b/TODO.md index b7fd994..db8fc29 100644 --- a/TODO.md +++ b/TODO.md @@ -16,7 +16,7 @@ Below is a TODO list for further development of `next-auth-pubkey`. - test all user configuration options - tidy up READMEs - add BTC address to contributors section of readme - - add suggestion: cleaning up old and unused pubkey session data that was created but never reached success state. + - add suggestion: cleaning up old and unused pubkey data that was created but never reached success state. ### Release diff --git a/examples/app-router/package-lock.json b/examples/app-router/package-lock.json index ffff6ab..bfc52aa 100644 --- a/examples/app-router/package-lock.json +++ b/examples/app-router/package-lock.json @@ -27,7 +27,7 @@ }, "../..": { "name": "next-auth-pubkey", - "version": "1.0.3", + "version": "1.0.4", "license": "ISC", "dependencies": { "@noble/curves": "^1.3.0", diff --git a/examples/app-router/utils/nextauth.ts b/examples/app-router/utils/nextauth.ts index 25098f5..ffae627 100644 --- a/examples/app-router/utils/nextauth.ts +++ b/examples/app-router/utils/nextauth.ts @@ -10,20 +10,19 @@ import storage from "node-persist"; // ⚠️ WARNING using node-persist is not await storage.init(); const config: NextAuthPubkeyConfig = { - // required baseUrl: env.NEXTAUTH_URL, secret: env.NEXTAUTH_SECRET, storage: { - async set({ k1, session }) { - await storage.setItem(`k1:${k1}`, session); + async set({ k1, data }) { + await storage.setItem(`k1:${k1}`, data); }, async get({ k1 }) { return await storage.getItem(`k1:${k1}`); }, - async update({ k1, session }) { + async update({ k1, data }) { const old = await storage.getItem(`k1:${k1}`); if (!old) throw new Error(`Could not find k1:${k1}`); - await storage.updateItem(`k1:${k1}`, { ...old, ...session }); + await storage.updateItem(`k1:${k1}`, { ...old, ...data }); }, async delete({ k1 }) { await storage.removeItem(`k1:${k1}`); diff --git a/examples/drizzle/README.md b/examples/drizzle/README.md index 777a2d8..b270490 100644 --- a/examples/drizzle/README.md +++ b/examples/drizzle/README.md @@ -1,6 +1,6 @@ ## About -This example uses the [Drizzle ORM](https://github.com/drizzle-team/drizzle-orm) to connect to a MySql database which is used for storage of pubkey auth session data. +This example uses the [Drizzle ORM](https://github.com/drizzle-team/drizzle-orm) to connect to a MySql database which is used for storage of pubkey data. ## Getting Started diff --git a/examples/drizzle/package-lock.json b/examples/drizzle/package-lock.json index bb04dda..3f6afdc 100644 --- a/examples/drizzle/package-lock.json +++ b/examples/drizzle/package-lock.json @@ -28,7 +28,7 @@ }, "../..": { "name": "next-auth-pubkey", - "version": "1.0.3", + "version": "1.0.4", "license": "ISC", "dependencies": { "@noble/curves": "^1.3.0", diff --git a/examples/drizzle/pages/api/pubkey/[...pubkey].ts b/examples/drizzle/pages/api/pubkey/[...pubkey].ts index c6f91f2..254f48f 100644 --- a/examples/drizzle/pages/api/pubkey/[...pubkey].ts +++ b/examples/drizzle/pages/api/pubkey/[...pubkey].ts @@ -8,12 +8,11 @@ import db from "@/utils/db"; import { env } from "@/env.mjs"; const config: NextAuthPubkeyConfig = { - // required baseUrl: env.NEXTAUTH_URL, secret: env.NEXTAUTH_SECRET, storage: { - async set({ k1, session }) { - await db.insert(pubkeyTable).values(session); + async set({ data }) { + await db.insert(pubkeyTable).values(data); }, async get({ k1 }) { const results: PubKey[] = await db @@ -23,10 +22,10 @@ const config: NextAuthPubkeyConfig = { return results[0]; }, - async update({ k1, session }) { + async update({ k1, data }) { const results = await db .update(pubkeyTable) - .set(session) + .set(data) .where(eq(pubkeyTable.k1, k1)); if (!results[0].affectedRows) throw new Error(`Could not find k1:${k1}`); }, diff --git a/examples/kv/README.md b/examples/kv/README.md index a5bbf2e..a92b79f 100644 --- a/examples/kv/README.md +++ b/examples/kv/README.md @@ -1,6 +1,6 @@ ## About -This example uses the Vercel [KV](https://vercel.com/docs/storage/vercel-kv) (Redis) for storage of pubkey auth session data. +This example uses the Vercel [KV](https://vercel.com/docs/storage/vercel-kv) (Redis) for storage of pubkey data. ## Getting Started diff --git a/examples/kv/package-lock.json b/examples/kv/package-lock.json index 301ce9e..cff1285 100644 --- a/examples/kv/package-lock.json +++ b/examples/kv/package-lock.json @@ -26,7 +26,7 @@ }, "../..": { "name": "next-auth-pubkey", - "version": "1.0.3", + "version": "1.0.4", "license": "ISC", "dependencies": { "@noble/curves": "^1.3.0", diff --git a/examples/kv/pages/api/pubkey/[...pubkey].ts b/examples/kv/pages/api/pubkey/[...pubkey].ts index f6a1510..572c224 100644 --- a/examples/kv/pages/api/pubkey/[...pubkey].ts +++ b/examples/kv/pages/api/pubkey/[...pubkey].ts @@ -6,20 +6,19 @@ import { kv } from "@vercel/kv"; import { env } from "@/env.mjs"; const config: NextAuthPubkeyConfig = { - // required baseUrl: env.NEXTAUTH_URL, secret: env.NEXTAUTH_SECRET, storage: { - async set({ k1, session }) { - await kv.set(`k1:${k1}`, session); + async set({ k1, data }) { + await kv.set(`k1:${k1}`, data); }, async get({ k1 }) { return await kv.get(`k1:${k1}`); }, - async update({ k1, session }) { + async update({ k1, data }) { const old = await kv.get(`k1:${k1}`); if (!old) throw new Error(`Could not find k1:${k1}`); - await kv.set(`k1:${k1}`, { ...old, ...session }); + await kv.set(`k1:${k1}`, { ...old, ...data }); }, async delete({ k1 }) { await kv.del(`k1:${k1}`); diff --git a/examples/plain-js/package-lock.json b/examples/plain-js/package-lock.json index 1f3ba4b..9791217 100644 --- a/examples/plain-js/package-lock.json +++ b/examples/plain-js/package-lock.json @@ -23,7 +23,7 @@ }, "../..": { "name": "next-auth-pubkey", - "version": "1.0.3", + "version": "1.0.4", "license": "ISC", "dependencies": { "@noble/curves": "^1.3.0", diff --git a/examples/plain-js/pages/api/pubkey/[...pubkey].js b/examples/plain-js/pages/api/pubkey/[...pubkey].js index d8257dc..fc29404 100644 --- a/examples/plain-js/pages/api/pubkey/[...pubkey].js +++ b/examples/plain-js/pages/api/pubkey/[...pubkey].js @@ -6,20 +6,19 @@ import storage from "node-persist"; // ⚠️ WARNING using node-persist is not await storage.init(); const config = { - // required baseUrl: process.env.NEXTAUTH_URL, secret: process.env.NEXTAUTH_SECRET, storage: { - async set({ k1, session }) { - await storage.setItem(`k1:${k1}`, session); + async set({ k1, data }) { + await storage.setItem(`k1:${k1}`, data); }, async get({ k1 }) { return await storage.getItem(`k1:${k1}`); }, - async update({ k1, session }) { + async update({ k1, data }) { const old = await storage.getItem(`k1:${k1}`); if (!old) throw new Error(`Could not find k1:${k1}`); - await storage.updateItem(`k1:${k1}`, { ...old, ...session }); + await storage.updateItem(`k1:${k1}`, { ...old, ...data }); }, async delete({ k1 }) { await storage.removeItem(`k1:${k1}`); diff --git a/examples/prisma/README.md b/examples/prisma/README.md index 8c8fb3e..b6003af 100644 --- a/examples/prisma/README.md +++ b/examples/prisma/README.md @@ -1,6 +1,6 @@ ## About -This example uses the [Prisma ORM](https://www.prisma.io/) to connect to a MySql database which is used for storage of pubkey auth session data. +This example uses the [Prisma ORM](https://www.prisma.io/) to connect to a MySql database which is used for storage of pubkey data. ## Getting Started diff --git a/examples/prisma/package-lock.json b/examples/prisma/package-lock.json index dbdea27..fb801d4 100644 --- a/examples/prisma/package-lock.json +++ b/examples/prisma/package-lock.json @@ -28,7 +28,7 @@ }, "../..": { "name": "next-auth-pubkey", - "version": "1.0.3", + "version": "1.0.4", "license": "ISC", "dependencies": { "@noble/curves": "^1.3.0", diff --git a/examples/prisma/pages/api/pubkey/[...pubkey].ts b/examples/prisma/pages/api/pubkey/[...pubkey].ts index df9bf85..55bd577 100644 --- a/examples/prisma/pages/api/pubkey/[...pubkey].ts +++ b/examples/prisma/pages/api/pubkey/[...pubkey].ts @@ -11,28 +11,19 @@ const config: NextAuthPubkeyConfig = { baseUrl: env.NEXTAUTH_URL, secret: env.NEXTAUTH_SECRET, storage: { - async set({ session }) { - await prisma.pubkey.create({ - data: session, - }); + async set({ data }) { + await prisma.pubkey.create({ data }); }, async get({ k1 }) { - const results = await prisma.pubkey.findUnique({ - where: { k1 }, - }); + const results = await prisma.pubkey.findUnique({ where: { k1 } }); if (!results) throw new Error(`Could not find k1:${k1}`); return results; }, - async update({ k1, session }) { - await prisma.pubkey.update({ - where: { k1 }, - data: session, - }); + async update({ k1, data }) { + await prisma.pubkey.update({ where: { k1 }, data }); }, async delete({ k1 }) { - await prisma.pubkey.delete({ - where: { k1 }, - }); + await prisma.pubkey.delete({ where: { k1 } }); }, }, generateQr, diff --git a/examples/ui-app-router/package-lock.json b/examples/ui-app-router/package-lock.json index c8a6cf5..85ab49f 100644 --- a/examples/ui-app-router/package-lock.json +++ b/examples/ui-app-router/package-lock.json @@ -27,7 +27,7 @@ }, "../..": { "name": "next-auth-pubkey", - "version": "1.0.3", + "version": "1.0.4", "license": "ISC", "dependencies": { "@noble/curves": "^1.3.0", diff --git a/examples/ui-app-router/utils/nextauth.ts b/examples/ui-app-router/utils/nextauth.ts index 7f7495a..b2ab7ce 100644 --- a/examples/ui-app-router/utils/nextauth.ts +++ b/examples/ui-app-router/utils/nextauth.ts @@ -10,28 +10,25 @@ import storage from "node-persist"; // ⚠️ WARNING using node-persist is not await storage.init(); const config: NextAuthPubkeyConfig = { - // required baseUrl: env.NEXTAUTH_URL, secret: env.NEXTAUTH_SECRET, storage: { - async set({ k1, session }) { - await storage.setItem(`k1:${k1}`, session); + async set({ k1, data }) { + await storage.setItem(`k1:${k1}`, data); }, async get({ k1 }) { return await storage.getItem(`k1:${k1}`); }, - async update({ k1, session }) { + async update({ k1, data }) { const old = await storage.getItem(`k1:${k1}`); if (!old) throw new Error(`Could not find k1:${k1}`); - await storage.updateItem(`k1:${k1}`, { ...old, ...session }); + await storage.updateItem(`k1:${k1}`, { ...old, ...data }); }, async delete({ k1 }) { await storage.removeItem(`k1:${k1}`); }, }, generateQr, - - // optional pages: { lightningSignIn: "/lightning-signin", nostrSignIn: "/nostr-signin", diff --git a/examples/ui-pages-router/package-lock.json b/examples/ui-pages-router/package-lock.json index 5713ebd..303ae12 100644 --- a/examples/ui-pages-router/package-lock.json +++ b/examples/ui-pages-router/package-lock.json @@ -27,7 +27,7 @@ }, "../..": { "name": "next-auth-pubkey", - "version": "1.0.3", + "version": "1.0.4", "license": "ISC", "dependencies": { "@noble/curves": "^1.3.0", diff --git a/examples/ui-pages-router/pages/api/pubkey/[...pubkey].ts b/examples/ui-pages-router/pages/api/pubkey/[...pubkey].ts index ebb4e96..39136ff 100644 --- a/examples/ui-pages-router/pages/api/pubkey/[...pubkey].ts +++ b/examples/ui-pages-router/pages/api/pubkey/[...pubkey].ts @@ -8,28 +8,25 @@ import storage from "node-persist"; // ⚠️ WARNING using node-persist is not await storage.init(); const config: NextAuthPubkeyConfig = { - // required baseUrl: env.NEXTAUTH_URL, secret: env.NEXTAUTH_SECRET, storage: { - async set({ k1, session }) { - await storage.setItem(`k1:${k1}`, session); + async set({ k1, data }) { + await storage.setItem(`k1:${k1}`, data); }, async get({ k1 }) { return await storage.getItem(`k1:${k1}`); }, - async update({ k1, session }) { + async update({ k1, data }) { const old = await storage.getItem(`k1:${k1}`); if (!old) throw new Error(`Could not find k1:${k1}`); - await storage.updateItem(`k1:${k1}`, { ...old, ...session }); + await storage.updateItem(`k1:${k1}`, { ...old, ...data }); }, async delete({ k1 }) { await storage.removeItem(`k1:${k1}`); }, }, generateQr, - - // optional pages: { lightningSignIn: "/lightning-signin", nostrSignIn: "/nostr-signin", diff --git a/package-lock.json b/package-lock.json index d40f46e..07b0c46 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "next-auth-pubkey", - "version": "1.0.2", + "version": "1.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "next-auth-pubkey", - "version": "1.0.3", + "version": "1.0.4", "license": "ISC", "dependencies": { "@noble/curves": "^1.3.0", diff --git a/package.json b/package.json index f69ab55..eef3928 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-auth-pubkey", - "version": "1.0.3", + "version": "1.0.4", "type": "module", "description": "A light-weight Lightning and Nostr auth provider for your Next.js app that's entirely self-hosted and plugs seamlessly into the next-auth framework.", "license": "ISC", diff --git a/src/index.ts b/src/index.ts index 2521b5e..db78039 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ export type { - StorageSession as NextAuthPubkeyStorageSession, + StorageData as NextAuthPubkeyStorageData, UserConfig as NextAuthPubkeyConfig, } from "./main/config/types"; export type { NextAuthPubkeyClientSession } from "./server/index"; diff --git a/src/main/config/index.ts b/src/main/config/index.ts index 1b87f94..0e669a0 100644 --- a/src/main/config/index.ts +++ b/src/main/config/index.ts @@ -1,4 +1,4 @@ -export type { StorageSession, UserConfig, Config, HardConfig } from "./types"; +export type { StorageData, UserConfig, Config, HardConfig } from "./types"; export { formatConfig } from "./default"; export { hardConfig } from "./hard"; diff --git a/src/main/config/types.ts b/src/main/config/types.ts index 0ab112f..2148144 100644 --- a/src/main/config/types.ts +++ b/src/main/config/types.ts @@ -37,7 +37,7 @@ export type HardConfig = { }; }; -export type StorageSession = { +export type StorageData = { k1: string; state: string; @@ -56,7 +56,7 @@ export type RequiredConfig = { set: ( args: { k1: string; - session: { + data: { k1: string; state: string; }; @@ -68,11 +68,11 @@ export type RequiredConfig = { args: { k1: string }, url: URL, config: Config - ) => Promise; + ) => Promise; update: ( args: { k1: string; - session: { + data: { pubkey: string; sig: string; success: boolean; diff --git a/src/main/handlers/callback.test.ts b/src/main/handlers/callback.test.ts index add2796..c3758d1 100644 --- a/src/main/handlers/callback.test.ts +++ b/src/main/handlers/callback.test.ts @@ -152,7 +152,7 @@ describe("callback handler", () => { config, }); expect(jest.mocked(config.storage.update)).toHaveBeenCalledWith( - { k1: validK1, session: { pubkey, sig, success: true } }, + { k1: validK1, data: { pubkey, sig, success: true } }, url, config ); diff --git a/src/main/handlers/callback.ts b/src/main/handlers/callback.ts index 24feabe..823fe68 100644 --- a/src/main/handlers/callback.ts +++ b/src/main/handlers/callback.ts @@ -104,7 +104,7 @@ export default async function handler({ try { await config.storage.update( - { k1, session: { pubkey, sig, success: true } }, + { k1, data: { pubkey, sig, success: true } }, url, config ); diff --git a/src/main/handlers/create.test.ts b/src/main/handlers/create.test.ts index 48d8459..94fa579 100644 --- a/src/main/handlers/create.test.ts +++ b/src/main/handlers/create.test.ts @@ -118,7 +118,7 @@ describe("create handler", () => { config, }); expect(jest.mocked(config.storage.set)).toHaveBeenCalledWith( - { k1: newK1, session: { k1: newK1, state } }, + { k1: newK1, data: { k1: newK1, state } }, url, config ); diff --git a/src/main/handlers/create.ts b/src/main/handlers/create.ts index 3d64dbd..6f4132e 100644 --- a/src/main/handlers/create.ts +++ b/src/main/handlers/create.ts @@ -44,7 +44,7 @@ export default async function handler({ const encoded = lnurlEncode(callbackUrl.toString()).toUpperCase(); try { - await config.storage.set({ k1, session: { k1, state } }, url, config); + await config.storage.set({ k1, data: { k1, state } }, url, config); } catch (e) { if (config.flags.diagnostics && config.flags.logs) { console.warn( diff --git a/src/main/handlers/diagnostics.ts b/src/main/handlers/diagnostics.ts index 5e30aca..cec9e05 100644 --- a/src/main/handlers/diagnostics.ts +++ b/src/main/handlers/diagnostics.ts @@ -1,6 +1,6 @@ import { randomBytes } from "crypto"; -import { Config, StorageSession } from "../config/types"; +import { Config, StorageData } from "../config/types"; import { HandlerArguments, HandlerReturn } from "../utils/handlers"; type Check = { @@ -10,8 +10,8 @@ type Check = { }; export function testField( - received: StorageSession | null | undefined, - expected: StorageSession | null | undefined, + received: StorageData | null | undefined, + expected: StorageData | null | undefined, field: "k1" | "state" | "pubkey" | "sig" | "success" ): Check { const state = received?.[field] !== expected?.[field] ? "failed" : "success"; @@ -51,7 +51,7 @@ export async function testSet( export async function testGet( expectedSession: { k1: string; state: string }, - getMethod: () => Promise, + getMethod: () => Promise, config: Config ): Promise { const checks: Check[] = []; @@ -92,7 +92,7 @@ export async function testGet( export async function testUpdate( expectedSession: { k1: string; state: string }, updateMethod: (override?: string) => Promise, - getMethod: () => Promise, + getMethod: () => Promise, config: Config ): Promise { const checks: Check[] = []; @@ -173,7 +173,7 @@ export async function testUpdate( export async function testDelete( deleteMethod: () => Promise, - getMethod: () => Promise, + getMethod: () => Promise, config: Config ): Promise { const checks: Check[] = []; @@ -258,11 +258,11 @@ export default async function handler({ const updateSession = { pubkey, sig, success: true }; const setMethod = async () => - await config.storage.set({ k1, session: setSession }, url, config); + await config.storage.set({ k1, data: setSession }, url, config); const getMethod = async () => await config.storage.get({ k1 }, url, config); const updateMethod = async (override?: string) => await config.storage.update( - { k1: override || k1, session: updateSession }, + { k1: override || k1, data: updateSession }, url, config );