blog-server/controllers/login.js

38 lines
1.1 KiB
JavaScript

var login = async (ctx, next) => {
// var name = ctx.params.name;
// ctx.response.body = `<h1>Hello, ${name}!</h1>`;
ctx.response.body="这是login"
next()
};
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()
}
module.exports = {
'GET /login': login,
'POST /login' : loginup
};