I am currently working on an authentication pipeline that has a similar process to OAuth.
If authentication is successful in the third-party authentication system, the value will be transferred to Success Direct url, which I want to communicate with my server to configure to proceed with self-authentication.
In this situation, I organized the api of the success redirect url into lambda and used Lambda's callback object to process responses and requests in a chain.
But the code below always returned the null value, and I have no idea why.
// index.js
const Auth = require('AuthRequest')
exports.handler = async function (event, context, callback) {
if (event.routeKey == 'POST /authentication/success') {
var body = Auth.parse_query_to_json(Auth.decode_base64(event.body));
if (body.resultCode == '0000') { // success
await Auth.get_auth_info(body, callback);
}
else { // failure
callback(null, {
statusCode: 200,
body: 'failure1'
});
}
}
else if (event.routeKey == 'POST /authentication/failure') {
callback(null, {
statusCode: 200,
body: 'failure2'
});
}
};
// authRequest.js
const superagent = require('superagent')
var get_auth_info = async function (body, callback) {
await superagent
.post(url)
.send({
...
})
.set('Content-Type', 'application/json;charset=utf-8')
.end(async function(err, res) {
if(err) {
callback({
statusCode: 200,
body: 'failure3'
});
}
else {
callback(null, res)
}
});
};
var create_user_auth = async function (req, callback) {
await superagent
.post('my-auth-api-uri')
.send({
})
.set('Content-Type', 'application/json;charset=utf-8')
.end(async function(err, res) {
if(err) {
callback({
statusCode: 200,
body: 'failure4'
});
}
else {
if(res.status == 200) {
callback(null, res)
}
else {
callback({
statusCode: 200,
body: 'failure5'
});
}
}
});
};
......
And this lambda funciton is triggered by a API Gateway and uses the event object of Payload 2.0 version using http api.
Is there anyone who knows the answer to this question?
CodePudding user response:
If you want sync
handler, then you should not using async function
. Docs explain that it should be:
exports.handler = function(event, context, callback) {
CodePudding user response:
Thanks to @Marcin, my code worked perfectly!
// index.js
const Auth = require('AuthRequest')
exports.handler = function (event, context, callback) {
if (event.routeKey == 'POST /authentication/success') {
var body = Auth.parse_query_to_json(Auth.decode_base64(event.body));
if (body.resultCode == '0000') { // success
Auth.get_auth_info(body, callback);
}
else { // failure
callback(null, {
statusCode: 200,
body: 'failure1'
});
}
}
else if (event.routeKey == 'POST /authentication/failure') {
callback(null, {
statusCode: 200,
body: 'failure2'
});
}
};
// authRequest.js
const superagent = require('superagent')
var get_auth_info = function (body, callback) {
superagent
.post(url)
.send({
...
})
.set('Content-Type', 'application/json;charset=utf-8')
.end(function(err, res) {
if(err) {
callback({
statusCode: 200,
body: 'failure3'
});
}
else {
callback(null, res)
}
});
};
var create_user_auth = function (req, callback) {
superagent
.post('my-auth-api-uri')
.send({
})
.set('Content-Type', 'application/json;charset=utf-8')
.end(function(err, res) {
if(err) {
callback({
statusCode: 200,
body: 'failure4'
});
}
else {
if(res.status == 200) {
callback(null, res)
}
else {
callback({
statusCode: 200,
body: 'failure5'
});
}
}
});
};
......