Browse Source

Fix registration

master
Tony Kovanen 9 years ago
parent
commit
e7a1fcf8c4
  1. 6
      package.json
  2. 36
      scripts/post-install.js

6
package.json

@ -12,9 +12,10 @@
"now": "./build/bin/now" "now": "./build/bin/now"
}, },
"dependencies": { "dependencies": {
"babel-runtime": "6.5.0",
"arr-flatten": "1.0.1", "arr-flatten": "1.0.1",
"array-unique": "0.2.1", "array-unique": "0.2.1",
"babel-plugin-transform-runtime": "6.5.2",
"babel-runtime": "6.5.0",
"commander": "2.9.0", "commander": "2.9.0",
"copy-paste": "1.1.4", "copy-paste": "1.1.4",
"fs-promise": "0.4.1", "fs-promise": "0.4.1",
@ -41,7 +42,8 @@
}, },
"scripts": { "scripts": {
"gulp": "gulp", "gulp": "gulp",
"test": "ava" "test": "ava",
"postinstall": "scripts/post-install.js"
}, },
"babel": { "babel": {
"presets": [ "presets": [

36
scripts/post-install.js

@ -1,22 +1,31 @@
import { homedir } from 'os';
import { join as pathJoin } from 'path';
import readline from 'readline'; import readline from 'readline';
import fs from 'fs-promise';
import fetch from 'isomorphic-fetch'; import fetch from 'isomorphic-fetch';
import { stringify as stringifyQuery } from 'querystring'; import { stringify as stringifyQuery } from 'querystring';
const rl = readline.createInterface({ const stdin = process.openStdin();
input: process.stdin,
output: process.stdout
});
function readEmail () { function readEmail () {
return new Promise((reject, resolve) => { return new Promise((resolve, reject) => {
rl.question('> Enter your email address: ', resolve); process.stdout.write('> Enter your email address: ');
stdin.on('data', (d) => {
resolve(d.toString().trim());
});
}); });
} }
async function getVerificationToken (email) { async function getVerificationToken (email) {
const data = JSON.stringify({ email });
const res = await fetch('http://localhost:3001/', { const res = await fetch('http://localhost:3001/', {
method: 'POST', method: 'POST',
body: { email } mode: 'cors',
headers: new Headers({
'Content-Type': 'application/json',
'Content-Length': data.length
}),
body: data
}); });
const body = await res.json(); const body = await res.json();
@ -29,7 +38,7 @@ async function verify (email, verificationToken) {
token: verificationToken token: verificationToken
}; };
const res = await fetch('http://localhost:3001?' + stringifyQuery(query)); const res = await fetch('http://localhost:3001/verify?' + stringifyQuery(query));
const body = await res.json(); const body = await res.json();
return body.token; return body.token;
@ -51,15 +60,16 @@ async function register () {
do { do {
await sleep(5000); await sleep(5000);
try { try {
const res = await verify(email, verificationToken); final = await verify(email, verificationToken);
final = res.token;
} catch (e) {} } catch (e) {}
} while (!final); } while (!final);
return { email, token: final }; return { email, token: final };
} }
register().then(({ email, token }) => { register().then((loginData) => {
console.log('got email', email); return fs.writeFile(pathJoin(homedir(), '.now.json'), JSON.stringify(loginData));
console.log('got final token', token); }).then(() => {
console.log('> Registration successful. `now` is now ready to use. Your credentials are stored in `~/.now.json`');
process.exit(0);
}); });

Loading…
Cancel
Save