我對 Node JS 非常陌生,現(xiàn)在我正在學習如何在 Rest API 中使用 JWT 身份驗證,我已經(jīng)在登錄函數(shù)中創(chuàng)建了令牌生成器,但是當我嘗試通過設置某個角色來驗證令牌時,如果該角色等于2我可以得到一個數(shù)據(jù)?,F(xiàn)在我遇到了麻煩,因為我無法驗證我的令牌登錄: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"}); } } });
如何在 Node JS JWT 身份驗證中設置角色?
慕妹3242003
2023-07-29 15:38:31