Skip to content

Commit

Permalink
renaming the next-auth-pubkey "session" argument so that it's not con…
Browse files Browse the repository at this point in the history
…fused with the "next-auth" session object
  • Loading branch information
jowo-io committed Oct 21, 2024
1 parent 0465612 commit d963bb5
Show file tree
Hide file tree
Showing 29 changed files with 82 additions and 101 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand All @@ -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
},

/**
Expand All @@ -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
},

/**
Expand All @@ -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
},

/**
Expand All @@ -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
},
},

Expand Down Expand Up @@ -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):

Expand All @@ -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}`);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/app-router/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions examples/app-router/utils/nextauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion examples/drizzle/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/drizzle/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions examples/drizzle/pages/api/pubkey/[...pubkey].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}`);
},
Expand Down
2 changes: 1 addition & 1 deletion examples/kv/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/kv/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions examples/kv/pages/api/pubkey/[...pubkey].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion examples/plain-js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions examples/plain-js/pages/api/pubkey/[...pubkey].js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion examples/prisma/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/prisma/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 6 additions & 15 deletions examples/prisma/pages/api/pubkey/[...pubkey].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/ui-app-router/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions examples/ui-app-router/utils/nextauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion examples/ui-pages-router/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d963bb5

Please sign in to comment.