email working

This commit is contained in:
Sean Yesmunt 2021-03-27 22:42:43 -04:00
parent 383353ecf1
commit b57b370d08
2 changed files with 7 additions and 28 deletions

View file

@ -1,20 +1,14 @@
import { resolveHref } from 'next/dist/next-server/lib/router/router';
import { renderSync } from 'sass'; import { renderSync } from 'sass';
const AUTH_TOKEN = process.env.COMMANDER_AUTH_TOKEN;
const API = 'https://api.lbry.com/'; const API = 'https://api.lbry.com/';
const USER_TAG = 'lbry-sec'; const USER_TAG = 'lbry-sec';
const USER_DOES_NOT_EXIST = 'USER_DOES_NOT_EXIST'; function buildUrl(email) {
let url = `${API}/list/subscribe?email=${encodeURIComponent(
function buildUrl(email, createUser) {
let url = `${API}/users/tag?auth_token=${AUTH_TOKEN}&emails=${encodeURIComponent(
email email
)}&tag=${USER_TAG}`; )}&tag=${USER_TAG}`;
if (createUser) {
url += `&create_user=true`;
}
return url; return url;
} }
@ -30,24 +24,15 @@ export default (req, res) => {
return fetch(buildUrl(email)) return fetch(buildUrl(email))
.then((res) => res.json()) .then((res) => res.json())
.then((res) => { .then((res) => {
if (res.data && res.data.failed_emails.length > 0) { if (res.error) {
throw Error(USER_DOES_NOT_EXIST); throw Error(res.error);
} }
res.statusCode = 200; res.statusCode = 200;
res.json({ success: true }); res.json({ success: true });
}) })
.catch((error) => { .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.statusCode = 400;
res.json({ error: JSON.stringify(error) }); res.json({ error });
}); });
}; };

View file

@ -27,13 +27,7 @@ export default function Home() {
email, email,
}), }),
}) })
.then((res) => { .then((res) => res.json())
if (res.status >= 200 && res.status < 300) {
return res.json();
} else {
throw Error(res.error);
}
})
.then((data) => { .then((data) => {
setEmailLoading(false); setEmailLoading(false);
setEmailSuccess(true); setEmailSuccess(true);