Tony Kovanen
9 years ago
2 changed files with 79 additions and 74 deletions
@ -0,0 +1,77 @@ |
|||
import { homedir } from 'os'; |
|||
import { join as pathJoin } from 'path'; |
|||
import readline from 'readline'; |
|||
import fs from 'fs-promise'; |
|||
import fetch from 'isomorphic-fetch'; |
|||
import { stringify as stringifyQuery } from 'querystring'; |
|||
|
|||
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: new 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); |
|||
}); |
|||
} |
@ -1,75 +1,3 @@ |
|||
import { homedir } from 'os'; |
|||
import { join as pathJoin } from 'path'; |
|||
import readline from 'readline'; |
|||
import fs from 'fs-promise'; |
|||
import fetch from 'isomorphic-fetch'; |
|||
import { stringify as stringifyQuery } from 'querystring'; |
|||
import login from '../lib/login'; |
|||
|
|||
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: new 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 }; |
|||
} |
|||
|
|||
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); |
|||
}); |
|||
login(); |
|||
|
Loading…
Reference in new issue