blog-server/controllers/login.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-11-12 23:30:26 +08:00
2019-10-28 08:21:03 +08:00
var login = async (ctx, next) => {
// var name = ctx.params.name;
// ctx.response.body = `<h1>Hello, ${name}!</h1>`;
ctx.response.body="这是login"
next()
};
2019-11-12 23:30:26 +08:00
var loginup = async (ctx,next) => {
// ctx.body="这是login,post,name:" + ctx.request.body.name
// console.log("post")
//返回token
console.log(ctx.request.body.name == 'admin' , ctx.request.body.pwd == "123456")
if(ctx.request.body.name == 'admin' && ctx.request.body.pwd == "123456"){
const token = jwt.sign({
name: ctx.request.body.name,
_id: 1
}, 'my_token', { expiresIn: '2h' });
// jwt.verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ8.eyJuYW1lIjoiYWRtaW4iLCJfaWQiOjEsImlhdCI6MTU3MzU1Mjg1MCwiZXhwIjoxNTczNTYwMDUwfQ.TmAMeDq58F9Tt6gIQ9uU_FUVk4CEOKM90tvzymJo4Oc", 'my_token', function (err, data) {
// if (err) console.log(err)
// console.log('解析的数据', data)
// })
ctx.body={code:0,token}
}else{
ctx.body={
code:1,
msg:"密码错误"
}
}
next()
}
2019-10-28 08:21:03 +08:00
module.exports = {
2019-11-12 23:30:26 +08:00
'GET /login': login,
'POST /login' : loginup
2019-10-28 08:21:03 +08:00
};