support login & logout & me
This commit is contained in:
parent
74396f8426
commit
2c3a402f51
@ -3,16 +3,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const fetch = require('isomorphic-fetch');
|
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');
|
const fs = require('fs-promise');
|
||||||
|
|
||||||
let session = undefined;
|
let session = undefined;
|
||||||
let fileSession = undefined;
|
let savedSession = undefined;
|
||||||
|
|
||||||
exports.loadSession = async function() {
|
exports.loadSession = async function() {
|
||||||
if (await fs.exists('.pushy')) {
|
if (await fs.exists('.pushy')) {
|
||||||
try {
|
try {
|
||||||
session = JSON.parse(await fs.readFile('.pushy', 'utf8'));
|
exports.replaceSession(JSON.parse(await fs.readFile('.pushy', 'utf8')));
|
||||||
|
savedSession = session;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to parse file `.pushy`. Try to remove it manually.');
|
console.error('Failed to parse file `.pushy`. Try to remove it manually.');
|
||||||
throw e;
|
throw e;
|
||||||
@ -30,26 +31,56 @@ exports.replaceSession = function(newSession) {
|
|||||||
|
|
||||||
exports.saveSession = async function(){
|
exports.saveSession = async function(){
|
||||||
// Only save on change.
|
// Only save on change.
|
||||||
if (session != fileSession) {
|
if (session !== savedSession) {
|
||||||
const current = session;
|
const current = session;
|
||||||
const data = JSON.stringify(current, null, 4);
|
const data = JSON.stringify(current, null, 4);
|
||||||
await fs.writeFile('.pushy', data, 'utf8');
|
await fs.writeFile('.pushy', data, 'utf8');
|
||||||
fileSession = current;
|
savedSession = current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function queryWithBody(method){
|
exports.closeSession = async function(){
|
||||||
return async function(api, body) {
|
if (await fs.exists('.pushy')) {
|
||||||
const resp = await fetch(host + api, {
|
await fs.unlink('.pushy');
|
||||||
method: 'post',
|
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: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-AccessToken': session ? session.token : '',
|
'X-AccessToken': session ? session.token : '',
|
||||||
},
|
},
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.get = queryWithoutBody('GET');
|
||||||
exports.post = queryWithBody('POST');
|
exports.post = queryWithBody('POST');
|
||||||
exports.put = queryWithBody('PUT');
|
exports.put = queryWithBody('PUT');
|
||||||
|
@ -8,6 +8,12 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"logout": {
|
||||||
|
|
||||||
|
},
|
||||||
|
"me": {
|
||||||
|
|
||||||
},
|
},
|
||||||
"help": {
|
"help": {
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
* Created by tdzl2003 on 2/13/16.
|
* Created by tdzl2003 on 2/13/16.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const {loadSession} = require('./api');
|
||||||
const userCommands = require('./user').commands;
|
const userCommands = require('./user').commands;
|
||||||
|
|
||||||
function printUsage({args}) {
|
function printUsage({args}) {
|
||||||
@ -20,5 +21,8 @@ const commands = {
|
|||||||
|
|
||||||
exports.run = function () {
|
exports.run = function () {
|
||||||
const argv = require('cli-arguments').parse(require('./cli.json'));
|
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 {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 = {
|
exports.commands = {
|
||||||
login: async function ({args}){
|
login: async function ({args}){
|
||||||
const username = args[0] || await question('user:');
|
const login = args[0] || await question('user:');
|
||||||
const password = args[1] || await question('password:', true);
|
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…
x
Reference in New Issue
Block a user