diff --git a/api/functions/login/login.js b/api/functions/login/login.js index cf731e9..83a7b5c 100644 --- a/api/functions/login/login.js +++ b/api/functions/login/login.js @@ -32,7 +32,6 @@ const loginHandler = async (req, res) => { algorithms: ['HS256'], }) const user_id = payload.user_id; - const existingKeys = await prisma.userKey.findMany({ where: { user_id }, select: { key: true } }); if (existingKeys.length >= 3) @@ -42,7 +41,7 @@ const loginHandler = async (req, res) => { return res.status(400).json({ status: 'ERROR', reason: "Wallet already linked" }); // Remove old linking for this key if existing - await prisma.userKey.delete({ + await prisma.userKey.deleteMany({ where: { key } }) @@ -69,25 +68,44 @@ const loginHandler = async (req, res) => { const user = await getUserByPubKey(key) if (user === null) { - const nostr_prv_key = generatePrivateKey(); - const nostr_pub_key = getPublicKey(nostr_prv_key); + // Check if user had a previous account using this wallet - const createdUser = await prisma.user.create({ - data: { - pubKey: key, - name: key, - avatar: `https://avatars.dicebear.com/api/bottts/${key}.svg`, - nostr_prv_key, - nostr_pub_key, - }, - }) - await prisma.userKey.create({ - data: { - key, - user_id: createdUser.id, + const oldAccount = await prisma.user.findFirst({ + where: { + pubKey: key } }); + if (oldAccount) { + await prisma.userKey.create({ + data: { + key, + name: "My original wallet key", + user_id: oldAccount.id, + } + }); + } else { + const nostr_prv_key = generatePrivateKey(); + const nostr_pub_key = getPublicKey(nostr_prv_key); + + const createdUser = await prisma.user.create({ + data: { + pubKey: key, + name: key, + avatar: `https://avatars.dicebear.com/api/bottts/${key}.svg`, + nostr_prv_key, + nostr_pub_key, + }, + }) + await prisma.userKey.create({ + data: { + key, + name: "My original wallet key", + user_id: createdUser.id, + } + }); + } + } // calc the hash of k1 diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 5042441..09cdc25 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -68,7 +68,8 @@ model User { } model UserKey { - key String @id + key String @id + name String @default("My new wallet key") user User? @relation(fields: [user_id], references: [id]) user_id Int?