From 709392996c569c90721839937b2ee44c4bfb6ac1 Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Wed, 17 Aug 2022 09:51:20 +0300 Subject: [PATCH 1/5] update: db schema --- api/functions/login/login.js | 1 + prisma/schema.prisma | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/api/functions/login/login.js b/api/functions/login/login.js index cf731e9..2e80195 100644 --- a/api/functions/login/login.js +++ b/api/functions/login/login.js @@ -84,6 +84,7 @@ const loginHandler = async (req, res) => { await prisma.userKey.create({ data: { key, + name: "My original wallet key", user_id: createdUser.id, } }); 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? From 4a5348717b68441096dbe74e90ce8ea980d9d597 Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Wed, 17 Aug 2022 10:02:15 +0300 Subject: [PATCH 2/5] logs --- api/functions/login/login.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/api/functions/login/login.js b/api/functions/login/login.js index 2e80195..9dda97d 100644 --- a/api/functions/login/login.js +++ b/api/functions/login/login.js @@ -32,9 +32,10 @@ const loginHandler = async (req, res) => { algorithms: ['HS256'], }) const user_id = payload.user_id; - + console.log(user_id); const existingKeys = await prisma.userKey.findMany({ where: { user_id }, select: { key: true } }); - + console.log(existingKeys); + console.log(key); if (existingKeys.length >= 3) return res.status(400).json({ status: 'ERROR', reason: "Can only link up to 3 wallets" }) @@ -59,6 +60,7 @@ const loginHandler = async (req, res) => { .json({ status: "OK" }) } catch (error) { + console.log(error); return res.status(400).json({ status: 'ERROR', reason: 'Invalid User Token' }) } } From c7223ea67a918e4cac176dbe836394ff95dda3b4 Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Wed, 17 Aug 2022 10:15:28 +0300 Subject: [PATCH 3/5] fix: delete old user key --- api/functions/login/login.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/api/functions/login/login.js b/api/functions/login/login.js index 9dda97d..d1dc18b 100644 --- a/api/functions/login/login.js +++ b/api/functions/login/login.js @@ -32,10 +32,8 @@ const loginHandler = async (req, res) => { algorithms: ['HS256'], }) const user_id = payload.user_id; - console.log(user_id); const existingKeys = await prisma.userKey.findMany({ where: { user_id }, select: { key: true } }); - console.log(existingKeys); - console.log(key); + if (existingKeys.length >= 3) return res.status(400).json({ status: 'ERROR', reason: "Can only link up to 3 wallets" }) @@ -43,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 } }) From ec10c750633805d322c670c2659332f86af4ee4a Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Wed, 17 Aug 2022 10:20:36 +0300 Subject: [PATCH 4/5] remove logs --- api/functions/login/login.js | 1 - 1 file changed, 1 deletion(-) diff --git a/api/functions/login/login.js b/api/functions/login/login.js index d1dc18b..ed048e1 100644 --- a/api/functions/login/login.js +++ b/api/functions/login/login.js @@ -58,7 +58,6 @@ const loginHandler = async (req, res) => { .json({ status: "OK" }) } catch (error) { - console.log(error); return res.status(400).json({ status: 'ERROR', reason: 'Invalid User Token' }) } } From 20e2b1ea75998d69fd544d9cc086a4baa86579a6 Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Fri, 19 Aug 2022 20:35:09 +0300 Subject: [PATCH 5/5] 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