This commit is contained in:
Alex Grintsvayg 2017-06-21 08:42:22 -04:00
parent d67f07bcbb
commit cb68c538ee
3 changed files with 67 additions and 19 deletions

View file

@ -57,12 +57,22 @@ export function doUserEmailNew(email) {
email: email, email: email,
}); });
lbryio lbryio
.call("user_email", "new", { email: email, send_verification_email: true }, "post") .call(
"user_email",
"new",
{ email: email, send_verification_email: true },
"post"
)
.catch(error => { .catch(error => {
if (error.xhr && error.xhr.status == 409) { if (error.xhr && error.xhr.status == 409) {
return lbryio.call("user_email", "resend_token", { email: email, only_if_expired: true }, "post"); return lbryio.call(
"user_email",
"resend_token",
{ email: email, only_if_expired: true },
"post"
);
} }
throw error throw error;
}) })
.then(() => { .then(() => {
dispatch({ dispatch({
@ -106,7 +116,12 @@ export function doUserEmailVerify(verificationToken) {
}; };
lbryio lbryio
.call("user_email", "confirm", { verification_token: verificationToken, email: email }, "post") .call(
"user_email",
"confirm",
{ verification_token: verificationToken, email: email },
"post"
)
.then(userEmail => { .then(userEmail => {
if (userEmail.is_verified) { if (userEmail.is_verified) {
dispatch({ dispatch({

View file

@ -17,7 +17,10 @@ const CONNECTION_STRING = process.env.LBRY_APP_API_URL
const EXCHANGE_RATE_TIMEOUT = 20 * 60 * 1000; const EXCHANGE_RATE_TIMEOUT = 20 * 60 * 1000;
lbryio.getExchangeRates = function() { lbryio.getExchangeRates = function() {
if (!lbryio._exchangeLastFetched || Date.now() - lbryio._exchangeLastFetched > EXCHANGE_RATE_TIMEOUT) { if (
!lbryio._exchangeLastFetched ||
Date.now() - lbryio._exchangeLastFetched > EXCHANGE_RATE_TIMEOUT
) {
lbryio._exchangePromise = new Promise((resolve, reject) => { lbryio._exchangePromise = new Promise((resolve, reject) => {
lbryio lbryio
.call("lbc", "exchange_rate", {}, "get", true) .call("lbc", "exchange_rate", {}, "get", true)
@ -43,7 +46,9 @@ lbryio.call = function(resource, action, params = {}, method = "get") {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.addEventListener("error", function(event) { xhr.addEventListener("error", function(event) {
reject(new Error(__("Something went wrong making an internal API call."))); reject(
new Error(__("Something went wrong making an internal API call."))
);
}); });
xhr.addEventListener("timeout", function() { xhr.addEventListener("timeout", function() {
@ -82,11 +87,23 @@ lbryio.call = function(resource, action, params = {}, method = "get") {
const fullParams = { auth_token: token, ...params }; const fullParams = { auth_token: token, ...params };
if (method == "get") { if (method == "get") {
xhr.open("get", CONNECTION_STRING + resource + "/" + action + "?" + querystring.stringify(fullParams), true); xhr.open(
"get",
CONNECTION_STRING +
resource +
"/" +
action +
"?" +
querystring.stringify(fullParams),
true
);
xhr.send(); xhr.send();
} else if (method == "post") { } else if (method == "post") {
xhr.open("post", CONNECTION_STRING + resource + "/" + action, true); xhr.open("post", CONNECTION_STRING + resource + "/" + action, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.setRequestHeader(
"Content-type",
"application/x-www-form-urlencoded"
);
xhr.send(querystring.stringify(fullParams)); xhr.send(querystring.stringify(fullParams));
} else { } else {
reject(new Error(__("Invalid method"))); reject(new Error(__("Invalid method")));
@ -103,7 +120,11 @@ lbryio.getAuthToken = () => {
}; };
lbryio.setAuthToken = token => { lbryio.setAuthToken = token => {
return keytar.setPassword("LBRY", "auth_token", token ? token.toString().trim() : null); return keytar.setPassword(
"LBRY",
"auth_token",
token ? token.toString().trim() : null
);
}; };
lbryio.getCurrentUser = () => { lbryio.getCurrentUser = () => {
@ -129,18 +150,21 @@ lbryio.authenticate = function() {
lbryio lbryio
.getAuthToken() .getAuthToken()
.then(token => { .then(token => {
if (!token || token.length > 60) if (!token || token.length > 60) {
{
return false; return false;
} }
// check that token works // check that token works
return lbryio return lbryio
.getCurrentUser() .getCurrentUser()
.then(() => { return true; }) .then(() => {
.catch(() => { return false; }); return true;
}) })
.then((isTokenValid) => { .catch(() => {
return false;
});
})
.then(isTokenValid => {
if (isTokenValid) { if (isTokenValid) {
return; return;
} }
@ -152,12 +176,22 @@ lbryio.authenticate = function() {
.then(status => { .then(status => {
// first try swapping // first try swapping
app_id = status.installation_id; app_id = status.installation_id;
return lbryio.call("user", "token_swap", {auth_token: "", app_id: app_id}, "post"); return lbryio.call(
"user",
"token_swap",
{ auth_token: "", app_id: app_id },
"post"
);
}) })
.catch((err) => { .catch(err => {
if (err.xhr.status == 403) { if (err.xhr.status == 403) {
// cannot swap. either app_id doesn't exist, or app_id already swapped. pretend its the former and create a new user. if we get another error, then its the latter // cannot swap. either app_id doesn't exist, or app_id already swapped. pretend its the former and create a new user. if we get another error, then its the latter
return lbryio.call("user", "new", {auth_token: "", language: "en", app_id: app_id}, "post"); return lbryio.call(
"user",
"new",
{ auth_token: "", language: "en", app_id: app_id },
"post"
);
} }
throw err; throw err;
}) })

View file

@ -68,8 +68,7 @@ export const selectUserIsVerificationCandidate = createSelector(
selectUserIsRewardApproved, selectUserIsRewardApproved,
selectEmailToVerify, selectEmailToVerify,
selectUser, selectUser,
(isEligible, isApproved, emailToVerify, user) => (isEligible, isApproved, emailToVerify, user) => emailToVerify && user
emailToVerify && user
); );
export const selectUserIsAuthRequested = createSelector( export const selectUserIsAuthRequested = createSelector(