From 1f77c4301a3fde5534f584e7015557f5328595db Mon Sep 17 00:00:00 2001 From: Jannis Redmann Date: Fri, 20 Jan 2017 13:23:21 +0100 Subject: [PATCH] verification: don't request a code twice (#4221) * verification: check if user has received code * verification: don't request a code twice * code style :lint: --- js/src/3rdparty/email-verification/index.js | 27 ++++++++++++++++++--- js/src/3rdparty/sms-verification/index.js | 27 ++++++++++++++++++--- js/src/modals/Verification/email-store.js | 6 ++++- js/src/modals/Verification/sms-store.js | 6 ++++- js/src/modals/Verification/store.js | 12 ++++++--- 5 files changed, 67 insertions(+), 11 deletions(-) diff --git a/js/src/3rdparty/email-verification/index.js b/js/src/3rdparty/email-verification/index.js index d49ba3197..b1a868b0b 100644 --- a/js/src/3rdparty/email-verification/index.js +++ b/js/src/3rdparty/email-verification/index.js @@ -18,8 +18,10 @@ import { stringify } from 'querystring'; export const isServerRunning = (isTestnet = false) => { const port = isTestnet ? 28443 : 18443; + return fetch(`https://email-verification.parity.io:${port}/health`, { - mode: 'cors', cache: 'no-store' + mode: 'cors', + cache: 'no-store' }) .then((res) => { return res.ok; @@ -29,11 +31,30 @@ export const isServerRunning = (isTestnet = false) => { }); }; +export const hasReceivedCode = (email, address, isTestnet = false) => { + const port = isTestnet ? 28443 : 18443; + const query = stringify({ email, address }); + + return fetch(`https://email-verification.parity.io:${port}/?${query}`, { + mode: 'cors', + cache: 'no-store' + }) + .then((res) => { + return res.ok; + }) + .catch(() => { + return false; // todo: check for 404 + }); +}; + export const postToServer = (query, isTestnet = false) => { const port = isTestnet ? 28443 : 18443; query = stringify(query); - return fetch(`https://email-verification.parity.io:${port}/?` + query, { - method: 'POST', mode: 'cors', cache: 'no-store' + + return fetch(`https://email-verification.parity.io:${port}/?${query}`, { + method: 'POST', + mode: 'cors', + cache: 'no-store' }) .then((res) => { return res.json().then((data) => { diff --git a/js/src/3rdparty/sms-verification/index.js b/js/src/3rdparty/sms-verification/index.js index ed890631f..3fd3b0512 100644 --- a/js/src/3rdparty/sms-verification/index.js +++ b/js/src/3rdparty/sms-verification/index.js @@ -18,8 +18,10 @@ import { stringify } from 'querystring'; export const isServerRunning = (isTestnet = false) => { const port = isTestnet ? 8443 : 443; + return fetch(`https://sms-verification.parity.io:${port}/health`, { - mode: 'cors', cache: 'no-store' + mode: 'cors', + cache: 'no-store' }) .then((res) => { return res.ok; @@ -29,11 +31,30 @@ export const isServerRunning = (isTestnet = false) => { }); }; +export const hasReceivedCode = (number, address, isTestnet = false) => { + const port = isTestnet ? 8443 : 443; + const query = stringify({ number, address }); + + return fetch(`https://sms-verification.parity.io:${port}/?${query}`, { + mode: 'cors', + cache: 'no-store' + }) + .then((res) => { + return res.ok; + }) + .catch(() => { + return false; // todo: check for 404 + }); +}; + export const postToServer = (query, isTestnet = false) => { const port = isTestnet ? 8443 : 443; query = stringify(query); - return fetch(`https://sms-verification.parity.io:${port}/?` + query, { - method: 'POST', mode: 'cors', cache: 'no-store' + + return fetch(`https://sms-verification.parity.io:${port}/?${query}`, { + method: 'POST', + mode: 'cors', + cache: 'no-store' }) .then((res) => { return res.json().then((data) => { diff --git a/js/src/modals/Verification/email-store.js b/js/src/modals/Verification/email-store.js index b382a0a57..d9e9578cd 100644 --- a/js/src/modals/Verification/email-store.js +++ b/js/src/modals/Verification/email-store.js @@ -21,7 +21,7 @@ import EmailVerificationABI from '~/contracts/abi/email-verification.json'; import VerificationStore, { LOADING, QUERY_DATA, QUERY_CODE, POSTED_CONFIRMATION, DONE } from './store'; -import { isServerRunning, postToServer } from '../../3rdparty/email-verification'; +import { isServerRunning, hasReceivedCode, postToServer } from '~/3rdparty/email-verification'; // name in the `BadgeReg.sol` contract const EMAIL_VERIFICATION = 'emailverification'; @@ -64,6 +64,10 @@ export default class EmailVerificationStore extends VerificationStore { return isServerRunning(this.isTestnet); } + checkIfReceivedCode = () => { + return hasReceivedCode(this.email, this.account, this.isTestnet); + } + requestValues = () => [ sha3.text(this.email) ] @action setEmail = (email) => { diff --git a/js/src/modals/Verification/sms-store.js b/js/src/modals/Verification/sms-store.js index b6d4fb6c9..13ab69dcd 100644 --- a/js/src/modals/Verification/sms-store.js +++ b/js/src/modals/Verification/sms-store.js @@ -21,7 +21,7 @@ import SMSVerificationABI from '~/contracts/abi/sms-verification.json'; import VerificationStore, { LOADING, QUERY_DATA, QUERY_CODE, POSTED_CONFIRMATION, DONE } from './store'; -import { isServerRunning, postToServer } from '../../3rdparty/sms-verification'; +import { isServerRunning, hasReceivedCode, postToServer } from '~/3rdparty/sms-verification'; // name in the `BadgeReg.sol` contract const SMS_VERIFICATION = 'smsverification'; @@ -63,6 +63,10 @@ export default class SMSVerificationStore extends VerificationStore { return isServerRunning(this.isTestnet); } + checkIfReceivedCode = () => { + return hasReceivedCode(this.number, this.account, this.isTestnet); + } + @action setNumber = (number) => { this.number = number; } diff --git a/js/src/modals/Verification/store.js b/js/src/modals/Verification/store.js index 6805cb57e..585e9e1bb 100644 --- a/js/src/modals/Verification/store.js +++ b/js/src/modals/Verification/store.js @@ -182,11 +182,17 @@ export default class VerificationStore { } chain - .then(() => { + .then(() => this.checkIfReceivedCode()) + .then((hasReceived) => { + if (hasReceived) { + return; + } + this.step = REQUESTING_CODE; - return this.requestCode(); + return this + .requestCode() + .then(() => awaitPuzzle(api, contract, account)); }) - .then(() => awaitPuzzle(api, contract, account)) .then(() => { this.step = QUERY_CODE; })