我对NodeJS和Socket.io相当陌生。我有一个名为 index.js 的主文件,该文件调用了辅助函数verifyToken
,但是它并不等待辅助函数的返回值,而是继续前进。
我添加了console.log来跟踪执行流程,如下所示:
文件:index.js
socket.on('authenticate', function(data, ack) {
var employeeID = data.employeeID;
var token = data.Token;
var tokenHelper = require("@helpers/tokenHelper"); //@helpers is an alias by using the module-alias package for helpers folder
//Connect with helper to verify token
var isValid = tokenHelper.verifyToken(employeeID, token);
if(isValid) {
socket.auth = true;
ack("Authenticated");
console.log("Authenticated");
}
else {
ack("Unauthorised");
console.log("Unauthorised");
}
});
文件:tokenHelper.js
var mysqlDatabase = require("@config/mysql");
module.exports = {
verifyToken: function(employeeID, token, response) {
var publicKey = fs.readFileSync(path.resolve("SOME_PATH"), "utf8");
var isValid = false;
//Verify options
var verifyOptions = {
issuer: issuer,
subject: subject,
audience: audience,
expiresIn: expiresIn,
algorithm: ["RS256"]
};
//Extract data from received payload
var receivedPayload = jwt.verify(token, publicKey, verifyOptions);
var receivedEmailAddress = receivedPayload.sub;
var receivedEmployeeID = receivedPayload.EmployeeID;
console.log("Received email: " + receivedEmailAddress);
console.log("Received id: " + receivedEmployeeID);
console.log("Employee id: " + employeeID);
//SQL Query to check EmployeeID in the database, verification of token is successful if entry is found in the database
if(results !== null) {
isValid = true;
console.log("Verification successful");
}
return isValid;
}
};
当前控制台日志:
收到的电子邮件:user@test.com
收到的ID:1
员工编号:1
未经授权
验证成功
预期的控制台日志:
收到的电子邮件:user@test.com
收到的ID:1
员工编号:1
验证成功
未经授权
答案 0 :(得分:1)
文件:tokenHelper.js
module.exports = {
verifyToken: async (employeeID, token, response) => {
try {
const publicKey = fs.readFileSync(path.resolve('SOME_PATH'), 'utf8');
let isValid = false;
const verifyOptions = {
issuer: issuer,
subject: subject,
audience: audience,
expiresIn: expiresIn,
algorithm: ['RS256'],
};
const receivedPayload = await jwt.verify(token, publicKey, verifyOptions);
const receivedEmailAddress = receivedPayload.sub;
const receivedEmployeeID = receivedPayload.EmployeeID;
console.log(
`Received email: ${receivedEmailAddress}, Received id: ${receivedEmployeeID} and Employee id: ${employeeID}`
);
if (results !== null) {
isValid = true;
console.log('Verification successful');
}
return isValid;
} catch (error) {
console.error(error);
}
},
};
文件:index.js
const tokenHelper = require('@helpers/tokenHelper');
socket.on('authenticate', async (data, ack) => {
try {
const employeeID = data.employeeID;
const token = data.Token;
var isValid = await tokenHelper.verifyToken(employeeID, token);
if (isValid) {
socket.auth = true;
ack('Authenticated');
console.log('Authenticated');
} else {
ack('Unauthorised');
console.log('Unauthorised');
}
} catch (error) {
console.error(error);
}
});
答案 1 :(得分:0)
您错过了代码中的回调。请用以下代码替换您的代码,并让我知道是否出现问题。
令牌助手:
module.exports = {
async verifyToken = (employeeID, token, response) => {
const publicKey = fs.readFileSync(path.resolve("SOME_PATH"), "utf8");
let isValid = false;
const verifyOptions = {
issuer: issuer,
subject: subject,
audience: audience,
expiresIn: expiresIn,
algorithm: ['RS256']
};
const receivedPayload = await jwt.verify(token, publicKey, verifyOptions);
const receivedEmailAddress = receivedPayload.sub;
const receivedEmployeeID = receivedPayload.EmployeeID;
console.log(`Received email: ${receivedEmailAddress}, Received id: ${receivedEmployeeID} and Employee id: ${employeeID}`);
if (results !== null) {
isValid = true;
console.log('Verification successful');
}
return isValid;
}
};
文件:index.js
const tokenHelper = require("@helpers/tokenHelper");
socket.on('authenticate', async (data, ack) => {
const employeeID = data.employeeID;
const token = data.Token;
var isValid = await tokenHelper.verifyToken(employeeID, token);
if (isValid) {
socket.auth = true;
ack('Authenticated');
console.log('Authenticated');
} else {
ack('Unauthorised');
console.log('Unauthorised');
}
});