fin reset pass api , add js standard style

This commit is contained in:
Jay 2017-06-06 23:35:31 +08:00
parent b390643a70
commit 609c2843fa
7 changed files with 57 additions and 6 deletions

View File

@ -11,8 +11,8 @@ const mem = require('./libs/memcache_lib')
// const sendmail = require('./libs/sendmail') // const sendmail = require('./libs/sendmail')
// init memcached connection // init memcached connection
mem.host = 'dyn.trj.tw' mem.host = config.memcache.host
mem.port = 24002 mem.port = config.memcache.port
mem.connect() mem.connect()
// init mysql connection pool // init mysql connection pool

View File

@ -17,6 +17,10 @@
"dbname": "lora" "dbname": "lora"
} }
}, },
"memcache":{
"host": "dyn.trj.tw",
"port": 24002
},
"smtp": { "smtp": {
"sys_mail": "admin@localhost", "sys_mail": "admin@localhost",
"host": "localhost", "host": "localhost",

View File

@ -22,5 +22,8 @@
}, },
"devDependencies": { "devDependencies": {
"standard": "^10.0.2" "standard": "^10.0.2"
},
"standard": {
"ignore": []
} }
} }

View File

@ -10,6 +10,7 @@ module.exports = {
CE0007: '查無使用者資料', CE0007: '查無使用者資料',
CE0008: '重設密碼類型輸入錯誤', CE0008: '重設密碼類型輸入錯誤',
CE0009: '請輸入重設密碼Token', CE0009: '請輸入重設密碼Token',
CE0010: '舊密碼不符合',
CE1000: 'Token驗證失敗', CE1000: 'Token驗證失敗',
CE2000: '使用者權限不足', CE2000: '使用者權限不足',

View File

@ -9,7 +9,7 @@ const crypto = require('../../libs/crypto.js')
// const so = require('../../libs/storeObject') // const so = require('../../libs/storeObject')
const mongo = require('../../libs/mongo_model.js') const mongo = require('../../libs/mongo_model.js')
const mem = require('../../libs/memcache_lib') const mem = require('../../libs/memcache_lib')
const msgMng = require('./msgManager') const msgMng = require('./MsgManager')
const sendmail = require('../../libs/sendmail') const sendmail = require('../../libs/sendmail')
router router
@ -139,7 +139,7 @@ router
} }
} }
}) })
.post('/user/forgotpass', async(c, n) => { .post('/forgotpass', async(c, n) => {
let arr = c.request.body let arr = c.request.body
if (!arr.data) throw 'CE0000' if (!arr.data) throw 'CE0000'
if (!arr.data.account) throw 'CE0001' if (!arr.data.account) throw 'CE0001'
@ -177,7 +177,7 @@ router
record: [] record: []
} }
}) })
.post('/user/resetpass', async(c, n) => { .put('/resetpass', async(c, n) => {
let arr = c.request.body let arr = c.request.body
if (!arr.data) throw 'CE0000' if (!arr.data) throw 'CE0000'
if (!arr.data.account) throw 'CE0001' if (!arr.data.account) throw 'CE0001'
@ -188,6 +188,49 @@ router
if (!arr.data.token) throw 'CE0009' if (!arr.data.token) throw 'CE0009'
} }
if (!arr.data.newpass) throw 'CE0002' if (!arr.data.newpass) throw 'CE0002'
if (arr.data.resettype === 'token') {
try {
let data = await mem.getVal(arr.data.token)
if (!data) throw 'CE1000'
let dataJson = JSON.parse(data)
if (dataJson.account !== arr.data.account) throw 'CE1000'
} catch (err) {
if (typeof err === 'string') throw err
c.serr = err
throw 'CE1000'
}
} else {
if (!await mongo.Token.checkToken(c.headers['x-auth-token'] || '')) throw 'CE1000'
let token = await mongo.Token.getToken(c.headers['x-auth-token'] || '')
if (token.object.account !== arr.data.account) throw 'CE2000'
try {
let query = 'select `password` from ??.?? where `account` = ?'
let param = ['lora', 'user', arr.data.account]
let data = await c.syncQuery(query, param)
if (data.length === 0) throw 'CE0007'
if (!crypto.comparePass(arr.data.password, data[0].password)) throw 'CE0010'
} catch (err) {
if (typeof err === 'string') throw err
c.serr = err
throw 'SE0001'
}
}
let newpass = crypto.genPassHash(arr.data.newpass)
try {
let query = 'update ??.?? set `password` = ? where `account` = ?'
let param = ['lora', 'user', newpass, arr.data.account]
await c.syncQuery(query, param)
} catch (err) {
if (typeof err === 'string') throw err
c.serr = err
throw 'SE0002'
}
c.body = {
record: []
}
}) })
module.exports = router module.exports = router

View File

@ -6,7 +6,7 @@ const koaBody = require('koa-body')
const mysql = require('../../libs/mysql_pool.js') const mysql = require('../../libs/mysql_pool.js')
// const so = require('../../libs/storeObject') // const so = require('../../libs/storeObject')
// const config = require('../../config.json') // const config = require('../../config.json')
const msgMng = require('./msgManager') const msgMng = require('./MsgManager')
// routes // routes
const accountApi = require('./account.js') const accountApi = require('./account.js')