From 2c3a402f51b29ca07bde1d8d8df6a3d6c1791a91 Mon Sep 17 00:00:00 2001 From: tdzl2003 Date: Sun, 14 Feb 2016 16:47:58 +0800 Subject: [PATCH] support login & logout & me --- local-cli/api.js | 51 +++++++++++++++++++++++++++++++++++++--------- local-cli/cli.json | 6 ++++++ local-cli/index.js | 6 +++++- local-cli/user.js | 42 ++++++++++++++++++++++++++++++++++---- 4 files changed, 90 insertions(+), 15 deletions(-) diff --git a/local-cli/api.js b/local-cli/api.js index 589c8f1..659f959 100644 --- a/local-cli/api.js +++ b/local-cli/api.js @@ -3,16 +3,17 @@ */ const fetch = require('isomorphic-fetch'); -const host = process.env.PUSHY_REGISTRY || 'http://pushy.reactnative.cn'; +let host = process.env.PUSHY_REGISTRY || 'http://pushy.reactnative.cn'; const fs = require('fs-promise'); let session = undefined; -let fileSession = undefined; +let savedSession = undefined; exports.loadSession = async function() { if (await fs.exists('.pushy')) { try { - session = JSON.parse(await fs.readFile('.pushy', 'utf8')); + exports.replaceSession(JSON.parse(await fs.readFile('.pushy', 'utf8'))); + savedSession = session; } catch (e) { console.error('Failed to parse file `.pushy`. Try to remove it manually.'); throw e; @@ -30,26 +31,56 @@ exports.replaceSession = function(newSession) { exports.saveSession = async function(){ // Only save on change. - if (session != fileSession) { + if (session !== savedSession) { const current = session; const data = JSON.stringify(current, null, 4); await fs.writeFile('.pushy', data, 'utf8'); - fileSession = current; + savedSession = current; } } -function queryWithBody(method){ - return async function(api, body) { - const resp = await fetch(host + api, { - method: 'post', +exports.closeSession = async function(){ + if (await fs.exists('.pushy')) { + await fs.unlink('.pushy'); + savedSession = undefined; + } + session = undefined; + host = process.env.PUSHY_REGISTRY || 'http://pushy.reactnative.cn'; +} + +async function query(url, options) { + const resp = await fetch(url, options); + const json = await resp.json(); + if (resp.status !== 200) { + throw Object.assign(new Error(json.message || json.error), {status: resp.status}); + } + return json; +} + +function queryWithoutBody(method) { + return function(api) { + return query(host + api, { + method, + headers: { + 'X-AccessToken': session ? session.token : '', + }, + }); + }; +} + +function queryWithBody(method) { + return function(api, body) { + return query(host + api, { + method, headers: { 'Content-Type': 'application/json', 'X-AccessToken': session ? session.token : '', }, body: JSON.stringify(body), }); - } + }; } +exports.get = queryWithoutBody('GET'); exports.post = queryWithBody('POST'); exports.put = queryWithBody('PUT'); diff --git a/local-cli/cli.json b/local-cli/cli.json index 97e849e..52e6e96 100644 --- a/local-cli/cli.json +++ b/local-cli/cli.json @@ -8,6 +8,12 @@ } } + }, + "logout": { + + }, + "me": { + }, "help": { diff --git a/local-cli/index.js b/local-cli/index.js index af06da3..d36266c 100644 --- a/local-cli/index.js +++ b/local-cli/index.js @@ -2,6 +2,7 @@ * Created by tdzl2003 on 2/13/16. */ +const {loadSession} = require('./api'); const userCommands = require('./user').commands; function printUsage({args}) { @@ -20,5 +21,8 @@ const commands = { exports.run = function () { const argv = require('cli-arguments').parse(require('./cli.json')); - commands[argv.command](argv).catch(err=>console.error(err.stack)); + + loadSession() + .then(()=>commands[argv.command](argv)) + .catch(err=>console.error(err.stack)); }; diff --git a/local-cli/user.js b/local-cli/user.js index ba8cd74..1f1ad3f 100644 --- a/local-cli/user.js +++ b/local-cli/user.js @@ -3,11 +3,45 @@ */ const {question} = require('./utils'); +const { + post, + get, + replaceSession, + saveSession, + closeSession, +} = require('./api'); +const crypto = require('crypto'); + +function md5(str) { + return crypto.createHash('md5').update(str).digest('base64'); +} exports.commands = { login: async function ({args}){ - const username = args[0] || await question('user:'); - const password = args[1] || await question('password:', true); - + const login = args[0] || await question('user:'); + const pwd = args[1] || await question('password:', true); + const {token} = await post('/user/login', { + login, + pwd: md5(pwd), + }); + replaceSession({token}); + await saveSession(); + console.log('OK.'); + }, + logout: async function (){ + await closeSession(); + console.log('Logged out.'); + }, + me: async function (){ + try { + const me = await get('/user/me'); + console.log(me); + } catch (e) { + if (e.status === 401) { + console.log('Not loggined.\nRun `pushy login` at your project directory to login.'); + } else { + throw e; + } + } } -} \ No newline at end of file +}