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';
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 });
});
};

View file

@ -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);