Browse Source

Merge pull request #1 from zeithq/add/registration

Add registration
master
Guillermo Rauch 9 years ago
parent
commit
154dc7f402
  1. 18
      gulpfile.js
  2. 76
      lib/login.js
  3. 3183
      npm-debug.log
  4. 6
      package.json
  5. 3
      scripts/post-install.js

18
gulpfile.js

@ -9,7 +9,8 @@ gulp.task('help', help);
gulp.task('compile', [
'compile-lib',
'compile-bin'
'compile-bin',
'compile-scripts'
]);
gulp.task('compile-lib', function () {
@ -39,6 +40,21 @@ gulp.task('compile-bin', function () {
.pipe(gulp.dest('build/bin'));
});
gulp.task('compile-scripts', function () {
return gulp.src('scripts/*')
.pipe(babel({
presets: ['es2015'],
plugins: [
'syntax-async-functions',
'transform-async-to-generator',
'transform-runtime'
]
}))
.pipe(ext.crop())
.pipe(gulp.dest('build/scripts'));
});
gulp.task('lint', function () {
return gulp.src([
'gulpfile.js',

76
lib/login.js

@ -1,9 +1,77 @@
import { prompt } from 'inquirer';
import { homedir } from 'os';
import { join as pathJoin } from 'path';
import readline from 'readline';
import fs from 'fs-promise';
import fetch from 'node-fetch';
import { stringify as stringifyQuery } from 'querystring';
export default function login () {
return new Promise((resolve, reject) => {
prompt([{ name: 'email', message: 'Enter your email' }], (data) => {
const stdin = process.openStdin();
function readEmail () {
return new Promise((resolve, reject) => {
process.stdout.write('> Enter your email address: ');
stdin.on('data', (d) => {
resolve(d.toString().trim());
});
});
}
async function getVerificationToken (email) {
const data = JSON.stringify({ email });
const res = await fetch('http://localhost:3001/', {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
},
body: data
});
const body = await res.json();
return body.token;
}
async function verify (email, verificationToken) {
const query = {
email,
token: verificationToken
};
const res = await fetch('http://localhost:3001/verify?' + stringifyQuery(query));
const body = await res.json();
return body.token;
}
function sleep (ms) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
});
}
async function register () {
const email = await readEmail();
const verificationToken = await getVerificationToken(email);
console.log('> Please follow the link in your email to log in.');
let final;
do {
await sleep(5000);
try {
final = await verify(email, verificationToken);
} catch (e) {}
} while (!final);
return { email, token: final };
}
export default function () {
register().then((loginData) => {
return fs.writeFile(pathJoin(homedir(), '.now.json'), JSON.stringify(loginData));
}).then(() => {
console.log('> Registration successful. `now` is now ready to use. Your credentials are stored in `~/.now.json`');
process.exit(0);
});
}

3183
npm-debug.log

File diff suppressed because it is too large

6
package.json

@ -14,10 +14,11 @@
"dependencies": {
"arr-flatten": "1.0.1",
"array-unique": "0.2.1",
"babel-plugin-transform-runtime": "6.5.2",
"babel-runtime": "6.5.0",
"commander": "2.9.0",
"copy-paste": "1.1.4",
"fs-promise": "0.4.1",
"inquirer": "0.12.0",
"ms": "0.7.1",
"node-fetch": "1.3.3",
"spdy": "3.2.0"
@ -41,7 +42,8 @@
},
"scripts": {
"gulp": "gulp",
"test": "ava"
"test": "ava",
"postinstall": "scripts/post-install.js"
},
"babel": {
"presets": [

3
scripts/post-install.js

@ -0,0 +1,3 @@
import login from '../lib/login';
login();
Loading…
Cancel
Save