From b57b370d08ec39fe8362588dc90fa112fe59a4c5 Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Sat, 27 Mar 2021 22:42:43 -0400 Subject: [PATCH] email working --- pages/api/email.js | 27 ++++++--------------------- pages/index.js | 8 +------- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/pages/api/email.js b/pages/api/email.js index 997bccc..52b7222 100644 --- a/pages/api/email.js +++ b/pages/api/email.js @@ -1,20 +1,14 @@ +import { resolveHref } from 'next/dist/next-server/lib/router/router'; import { renderSync } from 'sass'; -const AUTH_TOKEN = process.env.COMMANDER_AUTH_TOKEN; const API = 'https://api.lbry.com/'; const USER_TAG = 'lbry-sec'; -const USER_DOES_NOT_EXIST = 'USER_DOES_NOT_EXIST'; - -function buildUrl(email, createUser) { - let url = `${API}/users/tag?auth_token=${AUTH_TOKEN}&emails=${encodeURIComponent( +function buildUrl(email) { + let url = `${API}/list/subscribe?email=${encodeURIComponent( email )}&tag=${USER_TAG}`; - if (createUser) { - url += `&create_user=true`; - } - return url; } @@ -30,24 +24,15 @@ export default (req, res) => { return fetch(buildUrl(email)) .then((res) => res.json()) .then((res) => { - if (res.data && res.data.failed_emails.length > 0) { - throw Error(USER_DOES_NOT_EXIST); + if (res.error) { + throw Error(res.error); } res.statusCode = 200; res.json({ success: true }); }) .catch((error) => { - if (error.message === USER_DOES_NOT_EXIST) { - return fetch(buildUrl(email, true)) - .then((res) => res.json()) - .then((data) => { - res.statusCode = 200; - res.json({ success: true }); - }); - } - res.statusCode = 400; - res.json({ error: JSON.stringify(error) }); + res.json({ error }); }); }; diff --git a/pages/index.js b/pages/index.js index da39a3b..51055fb 100644 --- a/pages/index.js +++ b/pages/index.js @@ -27,13 +27,7 @@ export default function Home() { email, }), }) - .then((res) => { - if (res.status >= 200 && res.status < 300) { - return res.json(); - } else { - throw Error(res.error); - } - }) + .then((res) => res.json()) .then((data) => { setEmailLoading(false); setEmailSuccess(true);