blog-server/controllers/login.js

33 lines
819 B
JavaScript
Raw Normal View History

2019-11-12 23:30:26 +08:00
var loginup = async (ctx,next) => {
2019-11-13 10:59:06 +08:00
let arr;
//查询数据库 没有用户默认admin 123456
2019-11-13 20:17:51 +08:00
console.time("asd")
2019-11-13 10:59:06 +08:00
await dbs.find('admin').then((res)=>{
arr = res.data
})
if(arr.length == 0){
await dbs.add('admin',{name:"admin",pwd:"123456"})
await dbs.find('admin').then((res)=>{
arr = res.data
})
}
2019-11-19 13:37:11 +08:00
console.timeEnd("asd")
2019-11-13 10:59:06 +08:00
//判断用户名密码
2019-11-13 11:11:21 +08:00
if(ctx.request.body.name == arr[0].name && ctx.request.body.pwd == arr[0].pwd){
2019-11-13 10:59:06 +08:00
//颁发token
2019-11-12 23:30:26 +08:00
const token = jwt.sign({
name: ctx.request.body.name,
2019-11-13 20:17:51 +08:00
id: arr[0]._id
2019-11-12 23:30:26 +08:00
}, 'my_token', { expiresIn: '2h' });
ctx.body={code:0,token}
}else{
2019-11-13 10:59:06 +08:00
ctx.body={code:1}
2019-11-12 23:30:26 +08:00
}
next()
}
2019-10-28 08:21:03 +08:00
module.exports = {
2019-11-12 23:30:26 +08:00
'POST /login' : loginup
2019-10-28 08:21:03 +08:00
};