blog-server/controllers/login.js
2019-11-13 10:59:06 +08:00

31 lines
760 B
JavaScript

var loginup = async (ctx,next) => {
let arr;
//查询数据库 没有用户默认admin 123456
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
})
}
//判断用户名密码
if(ctx.request.body.name == arr[i].name && ctx.request.body.pwd == arr[i].pwd){
//颁发token
const token = jwt.sign({
name: ctx.request.body.name,
_id: 1
}, 'my_token', { expiresIn: '2h' });
ctx.body={code:0,token}
}else{
ctx.body={code:1}
}
next()
}
module.exports = {
'POST /login' : loginup
};