As I said at the beginning, I’m starting with studies at Cloud Functions.
After a while I found the solution that I post below, it can help other people.
I think it’s important to say that I did it and how
But another question arose: is there a Global Keys tool to make the code more secure?
IMPORTANT:
The response function doesn’t work, I don’t know if it’s something in my code or an error.
The asynchronous function did not work
Parse.Cloud.define(‘test’, function (request) {
// Get User Request
var user = request.user;
// Check if is authenticate
if(user == null) {
return 'not authenticate';
}
// Set a random number to future checked
var pin = Math.floor(Math.random() * (99999 - 10000)) + 10000;
// Save in data the number
const object = new Parse.Object('numberVerified');
object.set('number', request.params.number);
object.set('pin', pin);
const token = { sessionToken: user.getSessionToken() };
// Set a ACL rule
parseAcl = new Parse.ACL(user);
parseAcl.setPublicReadAccess(false);
parseAcl.setPublicWriteAccess(false);
object.setACL(parseAcl);
// Save
object.save(null, token);
// Request a external service
Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://externalapitosendsms.com',
headers: {
'Authorization': 'Bearer MY_SECRET_TOKEN',
'Content-Type': 'application/json',
},
body: {
from: '+19072000010',
to: ['19072120721'],
body: 'This is your confirmed code ' + pin,
}
}).then(function(httpResponse) {
return 'Sended';
}, function(httpResponse) {
return 'Request failed with response code ' + httpResponse.status;
});