From 49fe86ae3c281cca9fb0e8f93b09f6ca27c82e63 Mon Sep 17 00:00:00 2001 From: Ben Wheeler Date: Wed, 23 Oct 2019 17:02:57 -0400 Subject: [PATCH] username step and email step caches ignore api failures --- src/components/join-flow/email-step.jsx | 5 ++++- src/components/join-flow/username-step.jsx | 5 ++++- src/lib/validate.js | 20 +++++++++++--------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/components/join-flow/email-step.jsx b/src/components/join-flow/email-step.jsx index fa1e27400..250dde590 100644 --- a/src/components/join-flow/email-step.jsx +++ b/src/components/join-flow/email-step.jsx @@ -91,7 +91,10 @@ class EmailStep extends React.Component { // email is not in our cache return validate.validateEmailRemotely(email).then( remoteResult => { - this.emailRemoteCache[email] = remoteResult; + // cache result, if it successfully heard back from server + if (remoteResult.requestSucceeded) { + this.emailRemoteCache[email] = remoteResult; + } return remoteResult; } ); diff --git a/src/components/join-flow/username-step.jsx b/src/components/join-flow/username-step.jsx index 3610a09e7..aef6d5617 100644 --- a/src/components/join-flow/username-step.jsx +++ b/src/components/join-flow/username-step.jsx @@ -56,7 +56,10 @@ class UsernameStep extends React.Component { // username is not in our cache return validate.validateUsernameRemotely(username).then( remoteResult => { - this.usernameRemoteCache[username] = remoteResult; + // cache result, if it successfully heard back from server + if (remoteResult.requestSucceeded) { + this.usernameRemoteCache[username] = remoteResult; + } return remoteResult; } ); diff --git a/src/lib/validate.js b/src/lib/validate.js index 5df20b0e4..9131a5a3e 100644 --- a/src/lib/validate.js +++ b/src/lib/validate.js @@ -21,21 +21,21 @@ module.exports.validateUsernameRemotely = username => ( uri: `/accounts/checkusername/${username}/` }, (err, body, res) => { if (err || res.statusCode !== 200) { - resolve({valid: false, errMsgId: 'general.error'}); + resolve({requestSucceeded: false, valid: false, errMsgId: 'general.error'}); } switch (body.msg) { case 'valid username': - resolve({valid: true}); + resolve({requestSucceeded: true, valid: true}); break; case 'username exists': - resolve({valid: false, errMsgId: 'registration.validationUsernameExists'}); + resolve({requestSucceeded: true, valid: false, errMsgId: 'registration.validationUsernameExists'}); break; case 'bad username': // i.e., vulgar - resolve({valid: false, errMsgId: 'registration.validationUsernameNotAllowed'}); + resolve({requestSucceeded: true, valid: false, errMsgId: 'registration.validationUsernameNotAllowed'}); break; case 'invalid username': default: - resolve({valid: false, errMsgId: 'registration.validationUsernameNotAllowed'}); + resolve({requestSucceeded: true, valid: false, errMsgId: 'registration.validationUsernameNotAllowed'}); } }); }) @@ -50,7 +50,9 @@ module.exports.validateUsernameRemotely = username => ( module.exports.validatePassword = (password, username) => { if (!password) { return {valid: false, errMsgId: 'general.required'}; - } else if (password.length < 6) { + // get length of password, considering unicode symbols as single chars. + // see discussion at https://stackoverflow.com/a/54370584/2308190 + } else if (Array.from(password).length < 6) { return {valid: false, errMsgId: 'registration.validationPasswordLength'}; } else if (password === 'password') { return {valid: false, errMsgId: 'registration.validationPasswordNotEquals'}; @@ -86,16 +88,16 @@ module.exports.validateEmailRemotely = email => ( uri: '/accounts/check_email/' }, (err, body, res) => { if (err || res.statusCode !== 200 || !body || body.length < 1 || !body[0].msg) { - resolve({valid: false, errMsgId: 'general.apiError'}); + resolve({requestSucceeded: false, valid: false, errMsgId: 'general.apiError'}); } switch (body[0].msg) { case 'valid email': - resolve({valid: true}); + resolve({requestSucceeded: true, valid: true}); break; case 'Scratch is not allowed to send email to this address.': // e.g., bad TLD or block-listed case 'Enter a valid email address.': default: - resolve({valid: false, errMsgId: 'registration.validationEmailInvalid'}); + resolve({requestSucceeded: true, valid: false, errMsgId: 'registration.validationEmailInvalid'}); break; } });