diff --git a/lib/login.js b/lib/login.js index c324d2d..2996ebf 100644 --- a/lib/login.js +++ b/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.`); diff --git a/package.json b/package.json index 184f362..4cfdccb 100644 --- a/package.json +++ b/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",