2017-06-01 14:37:51 +00:00
|
|
|
const Router = require('koa-router');
|
|
|
|
const router = new Router();
|
|
|
|
|
|
|
|
// import tools
|
|
|
|
const crypto = require('../../libs/crypto.js');
|
2017-06-02 10:07:25 +00:00
|
|
|
const so = require('../../libs/storeObject');
|
2017-06-02 14:53:34 +00:00
|
|
|
const mongo = require('../../libs/mongo_model.js');
|
2017-06-01 14:37:51 +00:00
|
|
|
|
|
|
|
router
|
|
|
|
.post('/login', async(c, n) => {
|
|
|
|
let arr = c.request.body;
|
|
|
|
if (!arr.data) return c.body = 'ERR0000';
|
|
|
|
if (!arr.data.account) return c.body = 'ERR0001';
|
|
|
|
if (!arr.data.password) return c.body = 'ERR0002';
|
|
|
|
try {
|
|
|
|
let user = await c.syncQuery('select `uid`,`account`,`password`,`name`,`email` from ??.?? where `account` = ?', ['lora', 'user', arr.data.account])
|
|
|
|
|
|
|
|
if (user.length == 0) return c.body = 'user not exists';
|
|
|
|
|
|
|
|
if (!crypto.comparePass(arr.data.password, user[0].password)) return c.body = 'password not match';
|
|
|
|
|
|
|
|
delete user[0].password;
|
|
|
|
|
|
|
|
c.body = {
|
|
|
|
record: user
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return c.body = 'DB Query Error';
|
|
|
|
}
|
2017-06-02 10:07:25 +00:00
|
|
|
|
|
|
|
let u = c.body.record[0];
|
2017-06-02 14:53:34 +00:00
|
|
|
let token = new mongo.token({ object: u, expire: Date.now() });
|
|
|
|
token.save();
|
|
|
|
c.body.rt = {
|
|
|
|
token: {
|
|
|
|
id: token._id
|
|
|
|
}
|
2017-06-02 10:07:25 +00:00
|
|
|
}
|
2017-06-01 14:37:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
module.exports = router;
|