Skip to main content

Implementing Two-Factor Authentication on the Member Registration Screen

Overview​

This tutorial explains how to implement two-factor authentication on the member registration screen by issuing a random 6-digit authentication code during new member registration.

tip

In this tutorial, we will introduce two-factor authentication using email, but you can also implement two-factor authentication using SMS by integrating with Twilio using the same steps.

What you will learn​

To implement two-factor authentication during new member registration, you will need to follow the following steps:

Prerequisites​

This page assumes that you have already built a project with Kuroco and Nuxt.js.
If you haven't done so yet, please refer to the following tutorials.

tip

If you want to implement two-factor authentication using SMS, you will also need to follow the tutorial below.

info

This tutorial is written with the following versions:
Nuxt2: v2.15.8
Nuxt3: v3.8.0

Preparations​

Creating APIs​

We will use the Default API and the Internal API for internal processing.
Image from Gyazo

Creating the Internal API​

It is recommended to create separate APIs for endpoints that are only used internally in Kuroco.
Therefore, first, create a new API for internal use. If you have already added it, you can proceed to the next step.

Creating an API​

Click [Add] from the API in the Kuroco Admin panel.

Image from Gyazo

On the API creation screen, enter the following and click "Add".

Image from Gyazo

ItemSetting
TitleInternal
Version1.0
DescriptionAPI for internal use

The API has been created.

Image from Gyazo

Security configuration​

Next, configure security. Click [Security].

Image from Gyazo

Set the security to [Dynamic Access Token], and click [Save].

Image from Gyazo

After setting security to a dynamic access token, it is recommended to use the Login::token endpoint, but it can be ignored if it is for internal use only.

Image from Gyazo

Setting CORS​

Next, we will configure CORS. Click [Operation CORS].

Image from Gyazo

Click [Add Origin] for CORS_ALLOW_ORIGINS and add the following:

  • Admin panel URL

Click [Add Method] for CORS_ALLOW_METHODS and add the following:

  • GET
  • POST
  • OPTIONS

Check that [Allow Credentials] is selected for CORS_ALLOW_CREDENTIALS.

Image from Gyazo

Click [Save] if there are no problems.

Two-Factor Authentication for Member Registration​

Now, let's implement two-factor authentication for member registration. The following 3 steps are assumed for user operations from the frontend:

StepFrontend (User) OperationKuroco ProcessAPI Used
1Send basic information for member registrationCreate a provisional memberMember::invite
2Access the invitation email URL and click the button to send the authentication codeAccess the provisional member information using email_hash, add a one-time password, and send an emailApi::request_api
Member::invite
MemberProvisional::update
3Enter the one-time password and send itAccess the provisional member information using email_hash, and if the one-time password matches, register the memberApi::request_api
Member::invite
Member::insert

Creating Endpoints​

Click [Add New Endpoint] from the Default API to add endpoints.

Image from Gyazo

This time, we will create the following endpoints:

  • Endpoint for creating provisional members
  • Endpoint to call custom function to create and send OTP
  • Endpoint to check OTP and complete registration

Endpoint for creating provisional members​

ItemSetting
Path2step_member_invite
CategoryMember
ModelMember
Operationinvite

Endpoint to call custom function to create and send OTP​

ItemSetting
Pathset_and_send_otp
CategoryAPI
ModelApi
Operationrequest_api_post
nameset_and_send_otp

Endpoint to check OTP and complete registration​

ItemSetting
Pathcheck_otp_and_regist
CategoryAPI
ModelApi
Operationrequest_api_post
namecheck_otp_and_regist

Creating Internal Endpoints​

Click [Add New Endpoint] from the Internal API to add endpoints.

Image from Gyazo

This time, we will create the following endpoints:

  • Endpoint for updating provisional members
  • Endpoint for member registration

Endpoint for updating provisional members​

Create an endpoint for updating provisional members with the following settings:

ItemSetting
Pathpre_member_update
CategoryMember
ModelMemberProvisional
Operationupdate

Endpoint for member registration​

Create an endpoint for member registration with the following settings:

ItemSetting
Path2step_member_regist
CategoryMember
ModelMember
Operationinsert
default_group_idEnter the ID of the member group to apply.
You can confirm the group ID from Groups.
login_ok_flgCheck the box

Creating Custom Function​

Custom Function for Issuing and Sending OTP​

Create a custom function that generates a one-time password and its expiration date, updates temporary member information, and sends the one-time password to the user.

Click [Operations] -> [Custom function].

Image from Gyazo

Click [Add].

Image from Gyazo

Enter the following:

ItemSetting
Titleset_and_send_otp
Identifierset_and_send_otp
ProcessingCode below
set_and_send_otp
loading...
tip

If you want to send the one-time password via SMS, modify the to=$pre_member_info.data.email part of sendmail to to="$pre_member_info.data.ext_info.tel@twilio.r-cms.jp".

Custom Function for Matching Authentication Codes​

Create a custom function that registers a member using Member::insert when the one-time password matches.

Click [Operations] -> [Custom function].

Image from Gyazo

Click [Add].

Image from Gyazo

Enter the following:

ItemSetting
Titlecheck_otp_and_regist
Identifiercheck_otp_and_regist
ProcessingCode below
check_otp_and_regist
loading...

Verifying the Operation with SwaggerUI​

Once you have prepared the Kuroco API and custom function, use SwaggerUI to verify the operation.

Send a Member Registration Request​

Click /rcms-api/1/2step_member_invite in the SwaggerUI of the Default endpoint.

Image from Gyazo

Click [try it out].

Image from Gyazo

Enter the following in the Request body and click [Execute]:

{
"email": "example@dexample.com",
"ext_info": {
"name": "MyName",
"login_pwd": "********",
"tel": "00011112222"
}
}

Image from Gyazo

Check the 200 response.

Image from Gyazo

Copy the key from the invitation email.

Image from Gyazo

tip

You can edit the content of the invitation email using the template for Message Templates. Modify the link part to {$smarty.const.ROOT_URL}/login/2step_regist?key={$preregist_key} so that you can directly transition from the email.

Send a Request to Send the One-Time Password​

Click /rcms-api/1/set_and_send_otp.

Image from Gyazo

Click [try it out].

Image from Gyazo

Enter the following in the Request body and click [Execute]:

{
"email_hash": "KEY"
}

Image from Gyazo

caution

Replace "KEY" with the key copied from the invitation email.

Check the 200 response.

Image from Gyazo

Confirm the one-time password that will be sent to your email.

Image from Gyazo

Sending the One-Time Password​

Click on /rcms-api/1/check_otp_and_regist.

Image from Gyazo

Click on [try it out].

Image from Gyazo

Enter the following in the Request body and click [Execute].

{
"email_hash": "KEY",
"otp":"OTP"
}
caution

Replace "OTP" and "KEY" with the key from the invitation email that you copied earlier.

Image from Gyazo

Check the response with status code 200.

Image from Gyazo

When you navigate to the member information list, you can confirm that the user has been registered.

Image from Gyazo

This completes the configuration on the Kuroco side. Please implement the functionality in the frontend based on the behavior confirmed in SwaggerUI.

Implementing the Frontend​

Creating a Page for Pre-registration​

Create the following files to be displayed in the /login/2step_pre_regist/ directory.

/pages/login/2step_pre_regist/index.vue
loading...
caution

The provided sample code is minimal and for reference purposes only. Please add form validation logic as needed.

Creating a Page for Registration​

Create the following files to be displayed in the /login/2step_regist/ directory.

/pages/login/2step_regist/index.vue
loading...
info

The email_hash is received as a query parameter.

Testing​

Adjust the code according to your environment and perform testing. Once you have confirmed that it works, the implementation of two-step authentication for new member registration is complete.

Pre-registration​

Image from Gyazo

Additional Authentication Code Registration​

Image from Gyazo


Support

If you have any other questions, please contact us or check out Our Slack Community.