const Router = require('koa-router'); const router = new Router(); // import tools const crypto = require('../../libs/crypto.js'); const so = require('../../libs/storeObject'); const mongo = require('../../libs/mongo_model.js'); router .post('/login', async(c, n) => { let arr = c.request.body; if (!arr.data) throw 'CE0000'; if (!arr.data.account) throw 'CE0001'; if (!arr.data.password) throw 'CE0002'; try { let user = await c.syncQuery('select `uid`,`account`,`password`,`name`,`email` from ??.?? where `account` = ?', ['lora', 'user', arr.data.account]) if (user.length == 0) throw 'CE0003'; if (!crypto.comparePass(arr.data.password, user[0].password)) throw 'CE0003'; delete user[0].password; c.body = { record: user } } catch (err) { if(typeof err == 'string') throw err; c.serr = err; throw 'SE0000'; } let u = c.body.record[0]; let token = new mongo.token({ object: u, expire: Date.now() + 86400000 }); token.save(); c.body.rt = { token: { id: token._id } } }) module.exports = router;