From fda7d50b81d991b4010fa342b0c2ce3e4a744ab0 Mon Sep 17 00:00:00 2001 From: austinkelsay Date: Thu, 14 Nov 2024 15:51:29 -0600 Subject: [PATCH] Generate and save nostr keypair for email users on first signin --- src/components/profile/UserProfile.js | 12 +++++++----- src/components/profile/UserSettings.js | 10 ++++++---- src/pages/api/auth/[...nextauth].js | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/components/profile/UserProfile.js b/src/components/profile/UserProfile.js index 84cb0a5..18b3535 100644 --- a/src/components/profile/UserProfile.js +++ b/src/components/profile/UserProfile.js @@ -85,7 +85,7 @@ const UserProfile = () => {
user's avatar {

{user.username || user?.email || "Anon"}

-

- - {nip19.npubEncode(user.pubkey)} -

+ {user.pubkey && ( +

+ + {nip19.npubEncode(user.pubkey)} +

+ )} {user?.lightningAddress && (

Lightning Address: {user.lightningAddress.name}@plebdevs.com copyToClipboard(user.lightningAddress.name + "@plebdevs.com")} /> diff --git a/src/components/profile/UserSettings.js b/src/components/profile/UserSettings.js index b8f00b0..b04acee 100644 --- a/src/components/profile/UserSettings.js +++ b/src/components/profile/UserSettings.js @@ -192,7 +192,7 @@ const UserSettings = () => {
user's avatar {

{user.username || user?.email || "Anon"}

-

- + {user.pubkey && ( +

+ {nip19.npubEncode(user.pubkey)} -

+

+ )} {user?.lightningAddress && (

Lightning Address: {user.lightningAddress.name}@plebdevs.com copyToClipboard(user.lightningAddress.name + "@plebdevs.com")} /> diff --git a/src/pages/api/auth/[...nextauth].js b/src/pages/api/auth/[...nextauth].js index 68ee1ad..5298614 100644 --- a/src/pages/api/auth/[...nextauth].js +++ b/src/pages/api/auth/[...nextauth].js @@ -169,6 +169,23 @@ export const authOptions = { token.user = newUser; } + // if we sign up with email and we don't have a pubkey or privkey, we need to generate them + if (trigger === "signUp" && account?.provider === "email" && !user.pubkey && !user.privkey) { + const sk = generateSecretKey(); + const pubkey = getPublicKey(sk); + const privkey = bytesToHex(sk); + + // Update the user in the database + await prisma.user.update({ + where: { id: user.id }, + data: { pubkey, privkey } + }); + + // Update the user object + user.pubkey = pubkey; + user.privkey = privkey; + } + if (user) { token.user = user; if (user.pubkey && user.privkey) {