The 2-Factor Authentication package integrates Twilio services to provide an extra layer of security during the sign-in process. The package allows you to verify the identity of a site member logging in to your site.
In addition to an email and password, site members must enter a code sent to their phone when logging in. The phone number is associated with the site member during site registration.
Here is an example that demonstrates how to use this package.
Before using this package, set up the following:
Add a Members Area to your site.
Add the following Twilio keys from your Twilio account to the Secrets Manager:
velo-2fa-twilioAccountSid
: Your Twilio Account SIDvelo-2fa-twilioAuthToken
: Your Twilio Auth Tokenvelo-2fa-twilioVerifyServiceId
: Your Twilio Verification Service IDVelo Form: Change your member signup form to a Velo Form. Make sure to enable the Only allow backend calls for Signup and Login APIs advanced setting so that sensitive authentication information is not exposed.
Approval type: Decide whether to approve new members automatically or manually.
Navigate to Menus & Pages > Member Pages > Actions icon > Member Signup Settings. Select one of the following:
People I approve: Manually approve each member.
Everyone: Members are approved automatically.
The main functionality for this package is located in the backend files. In addition, the package contains an optional public file that uses the package functions to create a complete 2FA registration and login scenario. In order to use the public file, you’ll need to add the site elements listed in the How to Use the Package section below.
This package contains the following backend files:
The code in this file defines the registration and login functions used in the package. The functions in this file can be used in your page/public/backend code by importing them with the following syntax:
The file includes the following functions:
sendRegConfirmCode()
Sends a confirmation code to the site visitor's phone number if the site visitor is not registered yet.
Syntax:
Parameters:
A visitorInfo
object that contains details needed to send a confirmation code for registering the site visitor:
sms
or call
.Returns:
A promise that resolves to a response
object containing the following property:
If the promise is rejected, registerUser
is false
and the following property is also returned:
register()
Registers a member if the confirmation code is correct.
Syntax:
Parameters:
A registerInfo
object that contains details needed to send a confirmation code for registering the site visitor:
Returns:
A promise that resolves to a results
object containing the following property:
sendLoginConfirmCode()
Sends a confirmation code to the specified phone number if the site visitor is logged in.
Syntax:
Parameters:
A memberInfo
object that contains details needed to to send a confirmation code for logging in the member:
sms
or call
.Returns:
A promise that resolves to a response
object containing the following property:
If the promise is rejected, results
is false
and the following property is also returned:
login()
Logs the member in if the confirmation code is correct.
Parameters:
A loginInfo
object that contains details needed to send a confirmation code for logging the registered member in:
Returns:
A promise that resolves to a results
object containing the following property:
The code in this file sets and sorts the country code options. The function in this file can be used in your page/public/backend code by importing it with the following syntax:
The file includes the following function:
getCountries()
Sets and sorts the countries and their ISO3166 codes. Can be used as the options for a country code dropdown in the registration form.
Syntax:
Parameters:
None.
Returns:
A promise that resolves to an array of key-value pair objects containing country codes, such as:
Optional addition to the package with client-side code. The code in this file uses the package functions to register and login site members using 2-factor authentication. It assumes you have a registration/login lightbox on your site. See more details in Option 2 of the How to Use the Package section below.
The function in this file can be used in your page/public code by importing it with the following syntax:
The file includes the following function:
initPage()
Implements 2-factor authentication as site visitors interact with the elements in the registration/login lightbox in order to sign up and login to the site.
Syntax:
Parameter:
Returns:
Void.
This section demonstrates how to use the package functions to implement a typical 2FA registration and login scenario. See example site.
You have 2 options for using this package:
Option 1: Directly use the package functions in the login-wrapper.jsw backend module to build a custom login experience. This option provides you with more flexibility.
Option 2: Create a user interface and use the optional frontend code provided in the init-page.js public file. This option is easier to implement.
The following describes the general flow for using the package building blocks in the login-wrapper.jsw backend module to build a basic 2FA scenario:
sendRegConfirmCode()
to send a confirmation code.register()
to register the visitor, and handle success or error messages.sendLoginConfirmCode()
. If the site visitor is registered as a member, a confirmation code is sent to the phone number provided during registration.login()
to login the member, and handle success or error messages.To see example code for this flow, see the init-page.js public file in the package.
RegisterLogin
.Add the following elements:
Add the following elements:
Add the following code to your RegisterLogin
lightbox:
Import the initPage()
function from the package.
In the RegisterLogin
lightbox, set aliases for all the page components.
Call initPage()
with the page components.
This Velo package uses the following npm packages. To view the npm license, see the npm readmes.
1.0 Initial version.
Security, authentication, membersarea, twilio, 2fa