我对 Node JS 非常陌生,现在我正在学习如何在 Rest API 中使用 JWT 身份验证,我已经在登录函数中创建了令牌生成器,但是当我尝试通过设置某个角色来验证令牌时,如果该角色等于2我可以得到一个数据。现在我遇到了麻烦,因为我无法验证我的令牌
登录:
exports.login = function(req, res){
var post = {
email : req.body.email,
password : req.body.password
}
var query = "SELECT * FROM ?? WHERE ?? = ? AND ?? = ?";
var table = ["user", "password", md5(post.password), "email",post.email];
query = mysql.format(query,table);
connection.query(query, function(error, rows){
if(error){
console.log(error);
}else{
if (rows.length == 1){
//ini 1440 dalam second
var token = jwt.sign({rows}, config.secret);
id_user = rows[0].id;
username = rows[0].username;
email = rows[0].email;
tanggal_daftar = rows[0].tanggal_daftar;
role = rows[0].role;
var data = {
id_user : id_user,
access_token : token,
username : username,
email : email,
tanggal_daftar : tanggal_daftar,
role : role,
ip_address : ip.address()
}
res.json({
success : true,
message : "Token JWT generate",
token : token,
idUser: data.id_user,
username : username,
email : data.email,
tanggal_daftar : tanggal_daftar,
role : data.role
});
}else{
res.json({"Error": true, "Message":"Email atau password anda salah"});
}
}
});
缥缈止盈
相关分类