From 129cbd02e2b47b08b6d6961e043d2ca3d4eadab4 Mon Sep 17 00:00:00 2001 From: austinkelsay Date: Sat, 5 Oct 2024 16:37:44 -0500 Subject: [PATCH] Fix authorize function --- prisma/schema.prisma | 16 ++++++------ .../bitcoinConnect/CoursePaymentButton.js | 1 - .../bitcoinConnect/ResourcePaymentButton.js | 1 - src/middleware.js | 4 +-- src/pages/api/auth/[...nextauth].js | 25 ++++++++++--------- src/pages/course/[slug]/index.js | 1 + 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 61d1bc5..729ad31 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1,14 +1,14 @@ -// datasource db { -// provider = "postgresql" -// url = env("DATABASE_URL") -// } - datasource db { - provider = "postgresql" - url = env("POSTGRES_PRISMA_URL") - directUrl = env("POSTGRES_URL_NON_POOLING") + provider = "postgresql" + url = env("DATABASE_URL") } +// datasource db { +// provider = "postgresql" +// url = env("POSTGRES_PRISMA_URL") +// directUrl = env("POSTGRES_URL_NON_POOLING") +// } + generator client { provider = "prisma-client-js" } diff --git a/src/components/bitcoinConnect/CoursePaymentButton.js b/src/components/bitcoinConnect/CoursePaymentButton.js index e031a54..a5081ec 100644 --- a/src/components/bitcoinConnect/CoursePaymentButton.js +++ b/src/components/bitcoinConnect/CoursePaymentButton.js @@ -76,7 +76,6 @@ const CoursePaymentButton = ({ lnAddress, amount, onSuccess, onError, courseId } const result = await axios.post('/api/purchase/course', purchaseData); if (result.status === 200) { - showToast('success', 'Payment Successful', `Paid ${amount} sats and updated user purchases`); if (onSuccess) onSuccess(response); } else { throw new Error('Failed to update user purchases'); diff --git a/src/components/bitcoinConnect/ResourcePaymentButton.js b/src/components/bitcoinConnect/ResourcePaymentButton.js index 4794b98..134f740 100644 --- a/src/components/bitcoinConnect/ResourcePaymentButton.js +++ b/src/components/bitcoinConnect/ResourcePaymentButton.js @@ -73,7 +73,6 @@ const ResourcePaymentButton = ({ lnAddress, amount, onSuccess, onError, resource const result = await axios.post('/api/purchase/resource', purchaseData); if (result.status === 200) { - showToast('success', 'Payment Successful', `Paid ${amount} sats and updated user purchases`); if (onSuccess) onSuccess(response); } else { throw new Error('Failed to update user purchases'); diff --git a/src/middleware.js b/src/middleware.js index d0a6c3a..e4fefaf 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -10,7 +10,7 @@ const localRatelimit = { limit: async (key) => { const now = Date.now(); const windowMs = 10 * 1000; // 10 seconds - const maxRequests = 20; + const maxRequests = 25; const requestLog = inMemoryStore.get(key) || []; const windowStart = now - windowMs; @@ -36,7 +36,7 @@ const localRatelimit = { const ratelimit = process.env.NODE_ENV === 'production' ? new Ratelimit({ redis: kv, - limiter: Ratelimit.slidingWindow(20, '10 s'), + limiter: Ratelimit.slidingWindow(25, '10 s'), analytics: true, timeout: 1000, }) diff --git a/src/pages/api/auth/[...nextauth].js b/src/pages/api/auth/[...nextauth].js index 9db1626..ff58df9 100644 --- a/src/pages/api/auth/[...nextauth].js +++ b/src/pages/api/auth/[...nextauth].js @@ -29,21 +29,22 @@ const authorize = async (pubkey) => { if (dbUser) { const fields = await findKind0Fields(profile); // Only update 'avatar' or 'username' if they are different from kind0 fields on the dbUser - const updatedFields = ['avatar', 'username'].reduce((acc, key) => { - if (fields[key] !== dbUser[key]) { - acc[key] = fields[key]; + if (fields.avatar !== dbUser.avatar) { + const updatedUser = await updateUser(dbUser.id, { avatar: fields.avatar }); + if (updatedUser) { + dbUser = await getUserByPubkey(pubkey); + } + } else if (fields.username !== dbUser.username) { + const updatedUser = await updateUser(dbUser.id, { username: fields.username }); + if (updatedUser) { + dbUser = await getUserByPubkey(pubkey); } - return acc; - }, {}); - - // if there are updated fields, update the user only with the updated fields - if (Object.keys(updatedFields).length > 0) { - dbUser = await updateUser(dbUser.id, updatedFields); } - - // Combine user object with kind0Fields, giving priority to kind0Fields + // add the kind0 fields to the user const combinedUser = { ...dbUser, kind0: fields }; + console.log("COMBINED USER", combinedUser); + return combinedUser; } else { // Create user @@ -139,7 +140,7 @@ export const authOptions = { return null; } } - + if (user) { token.user = user; } diff --git a/src/pages/course/[slug]/index.js b/src/pages/course/[slug]/index.js index 970d96e..adebbf9 100644 --- a/src/pages/course/[slug]/index.js +++ b/src/pages/course/[slug]/index.js @@ -191,6 +191,7 @@ const Course = () => { }; const handlePaymentSuccess = async (response) => { + console.log("response in handlePaymentSuccess", response); if (response && response?.preimage) { const updated = await update(); console.log("session after update", updated);