Skip to content

Cleaning up OTP project for code exchange #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 23, 2024
Merged
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
23 changes: 7 additions & 16 deletions verify/functions/check-verify.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/**
* Check Verification
*
* This Function shows you how to check a verification token for Twilio Verify.
* Accepts a user-provided code and checks it against the verification token sent to the user.
* Wraps the /verification-check endpoint: https://www.twilio.com/docs/verify/api/verification-check
*
* 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)
*
* - Send an OTP to the user via the /start-verify function
*
* Returns JSON:
* {
Expand All @@ -19,24 +18,17 @@ exports.handler = async function (context, event, callback) {
const response = new Twilio.Response();
response.appendHeader('Content-Type', 'application/json');

/*
* 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');
*/

try {
if (typeof event.to === 'undefined' || typeof event.code === 'undefined') {
throw new Error('Missing parameter.');
}

const client = context.getTwilioClient();
const service = context.VERIFY_SERVICE_SID;
const { VERIFY_SERVICE_SID } = context;
const { to, code } = event;

const check = await client.verify
.services(service)
.services(VERIFY_SERVICE_SID)
.verificationChecks.create({ to, code });

if (check.status === 'approved') {
Expand All @@ -46,10 +38,9 @@ exports.handler = async function (context, event, callback) {
message: 'Verification success.',
});
return callback(null, response);
// eslint-disable-next-line no-else-return
} else {
throw new Error('Incorrect token.');
}

throw new Error('Incorrect token.');
} catch (error) {
console.error(error.message);
response.setBody({
Expand Down
28 changes: 4 additions & 24 deletions verify/functions/start-verify.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/**
* Start Verification
*
* This Function shows you how to send a verification token for Twilio Verify.
* Sends a verification token to the provided phone number or email.
* Wraps the /verification endpoint: https://www.twilio.com/docs/verify/api/verification
*
* 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
* {
Expand All @@ -19,40 +17,22 @@ exports.handler = async function (context, event, callback) {
const response = new Twilio.Response();
response.appendHeader('Content-Type', 'application/json');

/*
* 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');
*/

try {
if (typeof event.to === 'undefined' || event.to.trim() === '') {
throw new Error(
"Missing 'to' parameter; please provide a phone number or email."
);
}

/*
* DELETE THIS BLOCK IF YOU WANT TO ENABLE THE VOICE CHANNEL
* Learn more about toll fraud
* https://www.twilio.com/docs/verify/preventing-toll-fraud
*/
if (event.channel === 'call') {
throw new Error(
'Calls disabled by default. Update the code in <code>start-verify.js</code> to enable.'
);
}

const client = context.getTwilioClient();
const service = context.VERIFY_SERVICE_SID;
const { VERIFY_SERVICE_SID } = context;
const { to } = event;
const channel =
typeof event.channel === 'undefined' ? 'sms' : event.channel;
const locale = typeof event.locale === 'undefined' ? 'en' : event.locale;

const verification = await client.verify
.services(service)
.services(VERIFY_SERVICE_SID)
.verifications.create({
to,
channel,
Expand Down
4 changes: 2 additions & 2 deletions verify/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "1.0.1",
"version": "1.0.2",
"private": true,
"dependencies": {
"twilio": "^3.61.0"
"twilio": "^5.2.1"
}
}
19 changes: 0 additions & 19 deletions verify/tests/start-verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,6 @@ describe('verify/start-verification', () => {
startVerifyFunction(testContext, event, callback);
});

test('returns an error when "call" is the channel parameter', (done) => {
const callback = (_err, result) => {
expect(result).toBeDefined();
expect(result._body.success).toEqual(false);
expect(result._body.error).toEqual(
'Calls disabled by default. Update the code in <code>start-verify.js</code> to enable.'
);
expect(mockClient.verify.services).not.toHaveBeenCalledWith(
testContext.VERIFY_SERVICE_SID
);
done();
};
const event = {
to: '+17341234567',
channel: 'call',
};
startVerifyFunction(testContext, event, callback);
});

test('uses sms and english as the default parameters if not provied', (done) => {
const callback = (_err, result) => {
expect(result).toBeDefined();
Expand Down
Loading