From 20e2b1ea75998d69fd544d9cc086a4baa86579a6 Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Fri, 19 Aug 2022 20:35:09 +0300 Subject: [PATCH] udpate: handle old account same wallet --- api/functions/login/login.js | 50 ++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/api/functions/login/login.js b/api/functions/login/login.js index ed048e1..83a7b5c 100644 --- a/api/functions/login/login.js +++ b/api/functions/login/login.js @@ -68,26 +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, - name: "My original wallet 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