Skip to main content

Specify the membership group based on the domain at the time of member registration

Overview

This tutorial explains how to implement a function that specifies the member group based on the domain at the time of member registration. The endpoint for Member::insert cannot change default_group_id from the frontend due to security. Therefore, multiple endpoints are created, and this feature is implemented by distributing the endpoint to be used depending on the domain using custom function.

What you can learn

We will implement the feature to change the membership group based on the domain by following the steps below:

Prerequisites

In this tutorial, we will create endpoints and custom function, then perform the testing using Swagger UI.
Also, the groups to which the member will be assigned should already be created.

Refer to Setting up a new member registration form with Kuroco and Nuxt.js for implementation in the frontend.

API configuration

Creating an 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.
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.

Creating endpoints

Next, we will create the endpoints. We need two Member::insert endpoints with different default_group_id and an Api::request_api_post endpoint to call the custom function that we will be creating later.

First, we will create the the Member::insert endpoints. These endpoints will be used internally within Kuroco, so we will need to create them by clicking [Add new endpoint] in the "Internal" API.

Image from Gyazo

Create the following two endpoints:

FieldSetting
Pathmember/insert/a
CategoryMember
ModelMember
Operationinsert
default_group_idGroup ID to assign the member to (101)
login_ok_flgChecked

Image from Gyazo

FieldSettings
Pathmember/insert/b
CategoryMember
ModelMember
Operationinsert
default_group_idGroup ID to assign the member to (102)
login_ok_flgChecked

Image from Gyazo

Next, we need to create the Api::request_api_post endpoint.
This API will be used from the frontend so we will create the endpoint in the default API.
Click [Add new endpoint] in the "Default" API.

Image from Gyazo

ItemSettings
Pathmember/insert
CategoryAPI
ModelApi
Operationrequest_api_post
namemember_insert
(This will be used as the identifier of the custom function that we will create later.)

Image from Gyazo

The three endpoints we need now have been created.
Image from Gyazo
Image from Gyazo

Creating custom function

Next, create custom function to distribute endpoints to send requests to, depending on the domain. Click [Operation] -> [Custom function].

Image from Gyazo

Click [Add].

Image from Gyazo

Enter the following:

FieldSetting
TitleChange registration destination depending on domain
Identifiermember_insert
(This needs to be the same name that is set in the Api::request_api endpoint.)
ProcessingEnter the following code:
{* Change the registration destination depending on the domain of the posted email address. *}
{* Request body *}
{assign_array var='body' values=''}
{assign var='body' value=$smarty.post}

{* If the domain is not @example.com *}
{if $smarty.post.email|strpos:'@example.com' === false}
{api_internal
var='response'
status_var='status'
endpoint='/rcms-api/4/member/insert/a'
method='POST'
queries=$body
member_id='1'
}
{* If the domain is @example.com *}
{else}
{api_internal
var='response'
status_var='status'
endpoint='/rcms-api/4/member/insert/b'
method='POST'
queries=$body
member_id='1'
}
{/if}

{assign var=data value=$response}
caution

Change /rcms-api/4/ to your own endpoint.

Image from Gyazo

Click [Add] to save the custom function.

Testing

Finally, we will test that everything is working as expected.
Click [Swagger UI] of the Default API.

Image from Gyazo

Click [Try it out] for the created endpoint.

Image from Gyazo

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

{
"email": "example@example.com",
"name1":"Diverta",
"name2":"Taro",
"login_pwd":"********"
}

Image from Gyazo

Verify the message "Added" and Member ID are displayed.

Image from Gyazo

Next, enter the following into the Request body and click [Execute].

{
"email": "test@test.com",
"name1":"Diverta",
"name2":"Jiro",
"login_pwd":"********"
}

Image from Gyazo

Verify the message "Added" and Member ID are displayed.

Image from Gyazo

When you check the registered members on the member list page, you can confirm that the groups to which Diverta Taro and Diverta Jiro belong to are different.
Image from Gyazo

With this, the implementation of determining member group based on domain during member registration is complete.


Support

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