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:
This commit is contained in:
Jannis Redmann 2017-01-20 13:23:21 +01:00 committed by Gav Wood
parent 4e35fd215e
commit 1f77c4301a
5 changed files with 67 additions and 11 deletions

View File

@ -18,8 +18,10 @@ import { stringify } from 'querystring';
export const isServerRunning = (isTestnet = false) => { export const isServerRunning = (isTestnet = false) => {
const port = isTestnet ? 28443 : 18443; const port = isTestnet ? 28443 : 18443;
return fetch(`https://email-verification.parity.io:${port}/health`, { return fetch(`https://email-verification.parity.io:${port}/health`, {
mode: 'cors', cache: 'no-store' mode: 'cors',
cache: 'no-store'
}) })
.then((res) => { .then((res) => {
return res.ok; 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) => { export const postToServer = (query, isTestnet = false) => {
const port = isTestnet ? 28443 : 18443; const port = isTestnet ? 28443 : 18443;
query = stringify(query); 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) => { .then((res) => {
return res.json().then((data) => { return res.json().then((data) => {

View File

@ -18,8 +18,10 @@ import { stringify } from 'querystring';
export const isServerRunning = (isTestnet = false) => { export const isServerRunning = (isTestnet = false) => {
const port = isTestnet ? 8443 : 443; const port = isTestnet ? 8443 : 443;
return fetch(`https://sms-verification.parity.io:${port}/health`, { return fetch(`https://sms-verification.parity.io:${port}/health`, {
mode: 'cors', cache: 'no-store' mode: 'cors',
cache: 'no-store'
}) })
.then((res) => { .then((res) => {
return res.ok; 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) => { export const postToServer = (query, isTestnet = false) => {
const port = isTestnet ? 8443 : 443; const port = isTestnet ? 8443 : 443;
query = stringify(query); 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) => { .then((res) => {
return res.json().then((data) => { return res.json().then((data) => {

View File

@ -21,7 +21,7 @@ import EmailVerificationABI from '~/contracts/abi/email-verification.json';
import VerificationStore, { import VerificationStore, {
LOADING, QUERY_DATA, QUERY_CODE, POSTED_CONFIRMATION, DONE LOADING, QUERY_DATA, QUERY_CODE, POSTED_CONFIRMATION, DONE
} from './store'; } from './store';
import { isServerRunning, postToServer } from '../../3rdparty/email-verification'; import { isServerRunning, hasReceivedCode, postToServer } from '~/3rdparty/email-verification';
// name in the `BadgeReg.sol` contract // name in the `BadgeReg.sol` contract
const EMAIL_VERIFICATION = 'emailverification'; const EMAIL_VERIFICATION = 'emailverification';
@ -64,6 +64,10 @@ export default class EmailVerificationStore extends VerificationStore {
return isServerRunning(this.isTestnet); return isServerRunning(this.isTestnet);
} }
checkIfReceivedCode = () => {
return hasReceivedCode(this.email, this.account, this.isTestnet);
}
requestValues = () => [ sha3.text(this.email) ] requestValues = () => [ sha3.text(this.email) ]
@action setEmail = (email) => { @action setEmail = (email) => {

View File

@ -21,7 +21,7 @@ import SMSVerificationABI from '~/contracts/abi/sms-verification.json';
import VerificationStore, { import VerificationStore, {
LOADING, QUERY_DATA, QUERY_CODE, POSTED_CONFIRMATION, DONE LOADING, QUERY_DATA, QUERY_CODE, POSTED_CONFIRMATION, DONE
} from './store'; } from './store';
import { isServerRunning, postToServer } from '../../3rdparty/sms-verification'; import { isServerRunning, hasReceivedCode, postToServer } from '~/3rdparty/sms-verification';
// name in the `BadgeReg.sol` contract // name in the `BadgeReg.sol` contract
const SMS_VERIFICATION = 'smsverification'; const SMS_VERIFICATION = 'smsverification';
@ -63,6 +63,10 @@ export default class SMSVerificationStore extends VerificationStore {
return isServerRunning(this.isTestnet); return isServerRunning(this.isTestnet);
} }
checkIfReceivedCode = () => {
return hasReceivedCode(this.number, this.account, this.isTestnet);
}
@action setNumber = (number) => { @action setNumber = (number) => {
this.number = number; this.number = number;
} }

View File

@ -182,11 +182,17 @@ export default class VerificationStore {
} }
chain chain
.then(() => { .then(() => this.checkIfReceivedCode())
.then((hasReceived) => {
if (hasReceived) {
return;
}
this.step = REQUESTING_CODE; this.step = REQUESTING_CODE;
return this.requestCode(); return this
.requestCode()
.then(() => awaitPuzzle(api, contract, account));
}) })
.then(() => awaitPuzzle(api, contract, account))
.then(() => { .then(() => {
this.step = QUERY_CODE; this.step = QUERY_CODE;
}) })