support login & logout & me
This commit is contained in:
parent
74396f8426
commit
2c3a402f51
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
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 async function(api, body) {
|
||||
const resp = await fetch(host + api, {
|
||||
method: 'post',
|
||||
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');
|
||||
|
@ -8,6 +8,12 @@
|
||||
|
||||
}
|
||||
}
|
||||
},
|
||||
"logout": {
|
||||
|
||||
},
|
||||
"me": {
|
||||
|
||||
},
|
||||
"help": {
|
||||
|
||||
|
@ -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));
|
||||
};
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user