如何在路由获取令牌之前使用身份验证的中间件
how to use middleware of the auth before route to get token
我该如何解决这个问题?
我想在路由器中获取令牌,然后路由器在此中间件中发送 response.but 我的代码在路由后获取令牌 called.and 如何访问中间件令牌以验证用户
var express = require("express");
var request = require("request");
var router = express.Router();
var supertoken;
tokenmiddleware = function(req, res, next) {
console.log("this is auth middleware");
try {
var options = {
method: "POST",
url: "here is my auth server url",
headers: {
json: true,
Authorization: "", //
"Content-Type": "application/x-www-form-urlencoded"
}
},
form: {
grant_type: "password",
username: "usrename",
password: "password"
};
request(options, function(error, response, body1) {
if (error) {
throw new Error(error);
} else {
let info = JSON.parse(body1);
//it parse the body1 into json so we can use property of body1.
supertoken = info.access_token; //it gives the token of the super admin.
// console.log(supertoken)
// console.log(process.env.ACCESS_TOKEN);
//return supertoken
}
});
console.log("superrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
console.log(supertoken);
next();
} catch (error) {
return res.status(401).json({ message: "Auth Failed." });
}
}; //this middleware gave me a token.
router.post("/verifyUser", tokenmiddleware, (req, res) => {
//here i want my middleware token (but it calls after the route)
//here i applied logic of verify user but token is not working.(it say's undefined)
});
您的中间件包含请求,这是一个异步操作。你在请求回调之外调用 next()
。在你的中间件调用你在请求完成之前触发 next()
之后,只需将 next 移入请求回调
tokenmiddleware = function (req, res, next) {
console.log('this is auth middleware');
try {
var options = {
method: 'POST',
url: 'here is my auth server url',
headers: {
json: true,
Authorization: '', //
'Content-Type': 'application/x-www-form-urlencoded',
},
form: {
grant_type: 'password',
username: 'usrename',
password: 'password',
},
};
request(options, function(error, response, body1) {
if (error) {
throw new Error(error);
} else {
let info = JSON.parse(body1);
//it parse the body1 into json so we can use property of body1.
supertoken = info.access_token; //it gives the token of the super admin.
// console.log(supertoken)
console.log('superrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr');
console.log(supertoken);
req.userToken = info.access_token;
next();
}
});
} catch (error) {
return res.status(401).json({ message: 'Auth Failed.' });
}
};
router.post("/verifyUser", tokenmiddleware, (req, res) => {
console.log(req.userToken); // should be your token
});
我该如何解决这个问题? 我想在路由器中获取令牌,然后路由器在此中间件中发送 response.but 我的代码在路由后获取令牌 called.and 如何访问中间件令牌以验证用户
var express = require("express");
var request = require("request");
var router = express.Router();
var supertoken;
tokenmiddleware = function(req, res, next) {
console.log("this is auth middleware");
try {
var options = {
method: "POST",
url: "here is my auth server url",
headers: {
json: true,
Authorization: "", //
"Content-Type": "application/x-www-form-urlencoded"
}
},
form: {
grant_type: "password",
username: "usrename",
password: "password"
};
request(options, function(error, response, body1) {
if (error) {
throw new Error(error);
} else {
let info = JSON.parse(body1);
//it parse the body1 into json so we can use property of body1.
supertoken = info.access_token; //it gives the token of the super admin.
// console.log(supertoken)
// console.log(process.env.ACCESS_TOKEN);
//return supertoken
}
});
console.log("superrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
console.log(supertoken);
next();
} catch (error) {
return res.status(401).json({ message: "Auth Failed." });
}
}; //this middleware gave me a token.
router.post("/verifyUser", tokenmiddleware, (req, res) => {
//here i want my middleware token (but it calls after the route)
//here i applied logic of verify user but token is not working.(it say's undefined)
});
您的中间件包含请求,这是一个异步操作。你在请求回调之外调用 next()
。在你的中间件调用你在请求完成之前触发 next()
之后,只需将 next 移入请求回调
tokenmiddleware = function (req, res, next) {
console.log('this is auth middleware');
try {
var options = {
method: 'POST',
url: 'here is my auth server url',
headers: {
json: true,
Authorization: '', //
'Content-Type': 'application/x-www-form-urlencoded',
},
form: {
grant_type: 'password',
username: 'usrename',
password: 'password',
},
};
request(options, function(error, response, body1) {
if (error) {
throw new Error(error);
} else {
let info = JSON.parse(body1);
//it parse the body1 into json so we can use property of body1.
supertoken = info.access_token; //it gives the token of the super admin.
// console.log(supertoken)
console.log('superrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr');
console.log(supertoken);
req.userToken = info.access_token;
next();
}
});
} catch (error) {
return res.status(401).json({ message: 'Auth Failed.' });
}
};
router.post("/verifyUser", tokenmiddleware, (req, res) => {
console.log(req.userToken); // should be your token
});