AWS Lambda
'use strict';
const https = require('https');
// Enter your Form.io project API key here, which will provide AWS Lambda requests
// admin privilages.
let apiKey = '--- YOUR FORM.IO PROJECT API KEY ---';
// The lambda event execution context.
exports.handler = (event, context, callback) => {
// The user will make a request to Lambda and provide his/her JWT Token
// We will now use the "current" endpoint within our project to determine the
// full user object from that token.
const requestUser = https.request({
hostname: 'examples.form.io',
path: '/current',
method: 'GET',
headers: {
'x-jwt-token': event.jwtToken
}
}, (requestUserResponse) => {
let user = '';
requestUserResponse.setEncoding('utf8');
requestUserResponse.on('data', (chunk) => user += chunk);
requestUserResponse.on('end', () => {
// We now have the full user object. Parse it as a JSON object.
user = JSON.parse(user);
console.log(user);
// Here is where you could do something to validate the user...
// Such as, send a request to payment processor to validate payment
// token, etc.
// Say that this user is now valid.
user.data.valid = true;
// Now perform a PUT reqeust to update the user record as an administrator.
// We will use the x-token header which utilizes the Project API key to perform
// the update.
const updateUser = https.request({
hostname: 'examples.form.io',
path: '/user/submission/' + user._id,
method: 'PUT',
headers: {
'x-token': apiKey,
'Content-Type' : 'application/json'
}
}, (updateUserResponse) => {
// The user is now updated and valid.
callback(null, user);
});
updateUser.on('error', callback);
updateUser.end(JSON.stringify(user));
});
});
requestUser.on('error', callback);
requestUser.end();
};Last updated
Was this helpful?
