Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .twilio-functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"projects": {
"ACac33d4682e7b1a5e1f246746a7e6a8a5": {
"serviceSid": "ZS510b475a2c894d81d59e1b390ed37e02",
"latestBuild": "ZB70957d77fb020c2615d45dfec22e9e54"
}
},
"serviceSid": "ZS510b475a2c894d81d59e1b390ed37e02",
"latestBuild": "ZB70957d77fb020c2615d45dfec22e9e54"
}
32 changes: 30 additions & 2 deletions assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ <h3>Email</h3>
</form>
</div>
<div class="form-group col-md-3">
<button id="send_code" type="button" class="btn btn-info" onclick="sendValidation()">Validate Email</button>
<button id="send_code" type="button" class="btn btn-danger" onclick="sendCode('email')">Send Email</button>
</div>
</div>
Expand Down Expand Up @@ -135,7 +136,8 @@ <h4>Last Response</h4>

if (channel == 'email'){
to = $('#email').val();
}else{
}
else{
to = $('#phonenumber').val();
}

Expand Down Expand Up @@ -193,10 +195,36 @@ <h4>Last Response</h4>
$("#result_confirmCode").text(response.status);
});
}

function sendValidation() {
$("#result_sendCode").text('sending email validation');

var to = '';

to = $('#email').val();

var settings = {
"url": "/validate_email",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
},
"data": {
"email": to
}
};

$.ajax(settings).done(function (response) {
console.log(response);
$("#log_result").val(JSON.stringify(response, null, 2));
$("#result_sendCode").text('email validated');
});
}
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</body>
</html>
</html>ls
62 changes: 62 additions & 0 deletions assets/verify.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login with Twilio Verify</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous"
/>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"
></script>
</head>
<body>
<div class="container">
<h2>Twilio Verify</h2>
<div class="content">
<div id="result-message"></div>
<p>
<a href="/index.html">Try another verification</a>
</p>
</div>
</div><!-- /.container -->
</body>
<script>
function showVerificationStatus(alertType, message) {
const content = $("#result-message");
content.empty();
content.append($("<div>")
.addClass(`alert alert-${alertType}`)
.attr("role", "alert")
.text(message))
}

const params = new URLSearchParams(window.location.search);
const token = params.get("token");
const to = params.get("to");

// Twilio functions do not accept multipart/form-data
const data = new URLSearchParams();
data.append("to", to);
data.append("code", token);

fetch("./verify_bootstrap_confirm", {
method: 'POST',
body: data
})
.then(response => response.json())
.then(json => {
const alertType = json.success ? "success" : "danger";
showVerificationStatus(alertType, json.message);
})
.catch(err => {
console.log(err);
showVerificationStatus("danger", "Something went wrong!");
});
</script>
</html>
41 changes: 41 additions & 0 deletions functions/validate_email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
exports.handler = function(context, event, callback) {
const got = require('got');
const response = new Twilio.Response();
response.appendHeader('Content-Type', 'application/json');

response.appendHeader('Access-Control-Allow-Origin', '*');
response.appendHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');

if (typeof event.email === 'undefined') {
response.setBody({
"success": false,
"message": "Please Define Email"
})
response.setStatusCode(400);
return callback(null, response);
}

console.log(event.email);

got
.post('https://api.sendgrid.com/v3/validations/email', {
headers: {
'authorization': `Bearer ${context.SG_EMAIL_VERIFY_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({"email": event.email})
})
.then(resp => {
console.log('Body');
console.log(resp.body);
console.log(resp.headers);
response.setBody(resp.body);
callback(null, response);
})
.catch(err => {
response.setBody(err);
callback(response);
});

};
102 changes: 92 additions & 10 deletions functions/verify_bootstrap_confirm.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,96 @@
exports.handler = function(context, event, callback) {
// exports.handler = function(context, event, callback) {

// const client = context.getTwilioClient();

// console.log(event);

// client.verify.services(process.env.VERIFY_SERVICE)
// .verificationChecks
// .create ( {to: event.to, code: event.code})
// .then(verification_check => {
// console.log(verification_check.status);
// callback(null, verification_check);
// })
// .catch( err => {
// console.log(err);
// callback(err);
// });
// };

const client = context.getTwilioClient();

console.log(event);

/**
* Check Verification
*
* This Function shows you how to check a verification token for Twilio Verify.
*
* Pre-requisites
* - Create a Verify Service (https://www.twilio.com/console/verify/services)
* - Add VERIFY_SERVICE_SID from above to your Environment Variables (https://www.twilio.com/console/functions/configure)
* - Enable ACCOUNT_SID and AUTH_TOKEN in your functions configuration (https://www.twilio.com/console/functions/configure)
*
*
* Returns JSON:
* {
* "success": boolean,
* "message": string
* }
*/

exports.handler = function(context, event, callback) {
const response = new Twilio.Response();
response.appendHeader('Content-Type', 'application/json');

client.verify.services(process.env.VERIFY_SERVICE)
.verificationChecks
.create ( {to: event.to, code: event.code})
.then(verification_check => {
console.log(verification_check.status);
callback(null, verification_check);
// uncomment to support CORS
// response.appendHeader('Access-Control-Allow-Origin', '*');
// response.appendHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
// response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');

if (typeof event.to === 'undefined' ||
typeof event.code === 'undefined') {
response.setBody({
"success": false,
"message": "Missing parameter."
})
response.setStatusCode(400);
return callback(null, response);
}

const client = context.getTwilioClient();
const service = context.VERIFY_SERVICE;
const to = event.to;
const code = event.code;

client.verify.services(service)
.verificationChecks
.create({
to: to,
code: code
})
.then(check => {
if (check.status === "approved") {
response.setStatusCode(200);
response.setBody({
"success": true,
"message": "Verification success."
});
callback(null, response);
} else {
response.setStatusCode(401);
response.setBody({
"success": false,
"message": "Incorrect token."
});
callback(null, response);
}
})
.catch(error => {
console.log(error);
response.setStatusCode(error.status);
response.setBody({
success: false,
message: error.message
});
};
callback(null, response);
});
};
43 changes: 36 additions & 7 deletions functions/verify_bootstrap_sendcode.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
exports.handler = function(context, event, callback) {
const client = context.getTwilioClient();

const protocol = (context.DOMAIN_NAME.startsWith('localhost:') ? 'http' : 'https')
const callback_url = `${protocol}://${context.DOMAIN_NAME}${context.PATH.substr(0, context.PATH.lastIndexOf('/'))}/verify.html`

console.log(event);

client.verify.services(process.env.VERIFY_SERVICE)
.verifications
.create({ to: event.to,
locale : event.locale,
channel: event.channel
})
.then(verification => callback(null,verification));
if (event.channel == 'email') {
client.verify.services(process.env.VERIFY_SERVICE)
.verifications
.create({ to: event.to,
locale : event.locale,
channel: event.channel,
channelConfiguration: {
substitutions: { // used in email template
email: event.to,
callback_url: callback_url
}
}
})
.then(verification => callback(null,verification))
.catch( err => {
console.log(err);
callback(err);
});
} else {
client.verify.services(process.env.VERIFY_SERVICE)
.verifications
.create({ to: event.to,
locale : event.locale,
channel: event.channel
})
.then(verification => callback(null,verification))
.catch( err => {
console.log(err);
callback(err);
});
}


};
Loading