Browse Source

Merge pull request #52 from zeit/add/email-validation

Add email validation and input retrying
master
Naoyuki Kanezawa 9 years ago
parent
commit
d400c9de6c
  1. 26
      lib/login.js
  2. 3
      package.json

26
lib/login.js

@ -2,14 +2,24 @@ import chalk from 'chalk';
import fetch from 'node-fetch';
import * as cfg from './cfg';
import { stringify as stringifyQuery } from 'querystring';
import _emailRegex from 'email-regex';
function readEmail () {
const emailRegex = _emailRegex({ exact: true });
function readEmail ({ invalid = false } = {}) {
return new Promise((resolve, reject) => {
process.stdout.write('> Enter your email address: ');
const decorate = invalid ? chalk.red : (v) => v;
const prompt = decorate('> Enter your email address: ');
process.stdout.write(prompt);
const data = [];
process.stdin.on('data', (d) => {
process.stdin.pause();
resolve(d.toString().trim());
}).resume();
data.push(d);
if (d.indexOf('\n') > -1) {
process.stdin.pause();
resolve(Buffer.concat(data).toString().trim());
}
});
process.stdin.resume();
});
}
@ -49,8 +59,10 @@ function sleep (ms) {
});
}
async function register (url) {
const email = await readEmail();
async function register (url, { retryEmail = false } = {}) {
const email = await readEmail({ invalid: retryEmail });
if (!emailRegex.test(email)) return register(url, { retryEmail: true });
const verificationToken = await getVerificationToken(url, email);
console.log(`> Please follow the link sent to ${chalk.bold(email)} to log in.`);

3
package.json

@ -31,7 +31,8 @@
"socket.io-client": "1.4.5",
"split-array": "1.0.1",
"text-table": "0.2.0",
"spdy": "3.3.3"
"spdy": "3.3.3",
"email-regex": "1.0.0"
},
"devDependencies": {
"alpha-sort": "1.0.2",

Loading…
Cancel
Save