我是Express JS和Node JS的新手。
我打算手动在Express Middleware中实现Auth-Server 我正在使用https://github.com/ranm8/requestify发出请求
const requestify = require('requestify');
app.use(function (req, res, next) {
var config = require('./config-' + process.env.REACT_APP_ENV)
var redirect = config.ADMIN_UI_URL
// Perform login to Auth-Server
if (req.originalUrl.startsWith('/?code')) {
let auth_code = req.query.code
let params = {
'grant_type': 'authorization_code',
'code': auth_code,
'client_id': config.OAUTH_CLIENT_ID,
'redirect_uri': redirect
}
requestify.post(config.OAUTH_ID_URL+'/oauth2/token', params).then(function(response) {
console.log(response.getBody()); // There is not data post to the Auth-Server
});
return res.redirect('http://google.com');
}
// Check Token
if (req.cookies._token === undefined) {
let url = config.OAUTH_ID_URL + '/oauth2/authorize?' + 'response_type=code' + '&' + 'client_id=' + config.OAUTH_CLIENT_ID + '&' + 'redirect_uri=' + redirect
return res.redirect(url)
}
next()
})
我可以检查用户令牌并收到auth_code就好了。但我无法通过向Auth-User发送请求
来从登录用户请求令牌似乎我想念NodeJS,Epxress如何工作,或者某种范围在这里
请帮帮我!谢谢
答案 0 :(得分:0)
尝试检查您获得的错误。像这样添加错误处理程序
requestify.post(config.OAUTH_ID_URL+'/oauth2/token', params).then(function(response) {
console.log(response.getBody()); // There is not data post to the Auth-Server
}).error(function(error){
console.log(error) // see if anything is coming in error object
});
答案 1 :(得分:0)
我的不好,我应该使用https://www.npmjs.com/package/request#forms包
request.post({url:config.OAUTH_ID_URL+'/oauth2/token', form: bodyData},
function(err,httpResponse,body){
console.log(err);
console.log(body);
})
现在有效!!! 谢谢