Skip to main content

Creating a Subscription Form for Subscribers

Overview

This tutorial explains how to create a form to register and unsubscribe subscribers for a delivery feature in a Nuxt.js project using Kuroco.
The MagazineSubscriber::subscribe/unsubscribe endpoint for registering/unsubscribing from the delivery only requires posting the email, but if the URL of the endpoint is known, it is possible to register/unsubscribe someone else's email address.

Therefore, in this tutorial, we will introduce two methods: requiring login and using a key to unsubscribe from the delivery.

What you will learn

We will implement the following two operations to register and unsubscribe subscribers for the delivery:

Prerequisites

This page assumes that a project using Kuroco and Nuxt.js has already been set up.
If you haven't done so yet, please refer to the following tutorial.

info

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

Require login to register and unsubscribe from the delivery

We will implement the functionality for registering and unsubscribing from deliveries to work only when logged in.
The endpoint set to require login (self_only configuration) does not work when posting email, but it will be used to register and unsubscribe from deliveries by posting the user's own member_id while logged in.

Registering the endpoints

Click on [Add new endpoint] to create the endpoint for registering the delivery and the login endpoint.

Image from Gyazo

Endpoint for registering the delivery

Set the following information and click [Add].

FieldSetting
Pathmagazine_subscribe
CategoryDelivery
ModelMagazineSubscriber
Operationsubscribe
allow_magazine_id1
self_onlyCheck the box

Endpoint for unsubscribing from the delivery

Set the following information and click [Add].

FieldSetting
Pathmagazine_unsubscribe
CategoryDelivery
ModelMagazineSubscriber
Operationsubscribe
allow_magazine_id1
self_onlyCheck the box

Login endpoint

Use the default login endpoint that is already set up. If it does not exist, create a login endpoint with the following information:

FieldSetting
Pathlogin
CategoryAuthentication
ModelLogin
Operationlogin_challenge

Profile endpoint

Use the default profile endpoint that is already set up. If it does not exist, create a profile endpoint with the following information:

FieldSetting
Pathprofile
CategoryAuthentication
ModelLogin
Operationprofile

Frontend implementation

Next, create a form for subscribing to the magazine delivery on the frontend.
Create the following files so that they can be displayed in the /subscribe_with_login directory.

Additionally, the error messages are displayed as-is from Kuroco by receiving the response of the post process errors.

/pages/subscribe_with_login/index.vue
loading...
caution

The above sample code is minimal for reference purposes.
When using it in practice, please also use libraries for validation processing and login functionality.

Confirming the operation

Subscribe

Run npm run dev to confirm the operation.
After logging in at the URL /subscribe_with_login, when you click [Subscribe], an API request is made to the Kuroco endpoint, and you can see the message "Registered".

Image from Gyazo

When you check the Subscribers page, you can see that the subscriber has been registered.

Image from Gyazo

Unsubscribe

Next, when you click [Unsubscribe], an API request is made to the Kuroco endpoint, and you can see the message "Resigned".

Image from Gyazo

On the Subscribers page, you can see that the subscriber has been removed.

Image from Gyazo

Making subscription registration and cancellation without login required

Modify the implementation so that subscription registration can be done with just an email, and subscription cancellation can be done by clicking the [Unsubscribe] link in the subscription message.
For unsubscribing without logging in, use the key assigned to each subscriber to prevent unauthorized cancellation of other people's subscriptions.

You can check the key on the Subscribers page in the management screen.

Image from Gyazo

Registering Endpoints

Click on [Add New Endpoint] to create endpoints for subscription registration and login.

Image from Gyazo

Subscription Registration Endpoint

Set the following information and click on "Add".

FieldSetting
Pathmagazine_subscribe_with_key
CategorySubscription
ModelMagazineSubscriber
Operationsubscribe
allow_magazine_id1

Subscription Unsubscribe Endpoint

Set the following information and click on "Add".

FieldSetting
Pathmagazine_unsubscribe_with_key
CategorySubscription
ModelMagazineSubscriber
Operationsubscribe
allow_magazine_id1
required_keyCheck the box

Creating Custom Processing

The key for unsubscribing from the subscription is displayed in Subscribers page. Create a custom processing to make this key usable within the subscription message.

Click on [Operation] -> [Custom function].

Image from Gyazo

Click on [Add].

Image from Gyazo

Enter the following information:

FieldSetting
TitleInsertion of Magazine (to make key and email usable)
Identifiersubstitution_key
Component using thisTrigger: Insertion of Subscription
Value: 1
ProcessingThe following code
{assign_array var=substitutions values=''}
{assign var=substitutions.key value=$member_info.key}
{assign var=substitutions.email value=$member_info.email}

Frontend Implementation

Create a page for subscription registration and a page for subscription unsubscribe in the frontend.

First, create a page for subscription registration.
Create the following files so that they can be displayed in the /subscribe_with_key directory.

/pages/subscribe_with_key/index.vue
loading...

Next, create a page for subscription unsubscribe.
Create the following files so that they can be displayed in the /subscribe_with_key/unsubscribe directory.

The page for subscription unsubscribe assumes that it will be accessed through a link specified in the subscription message.

/pages/subscribe_with_key/unsubscribe.vue
loading...

Confirmation of operation

Register for delivery

Run npm run dev to confirm the operation.
When you enter an email address in the URL /subscribe_with_key and click [Subscribe], an API request is made to Kuroco's endpoint, and you can see the message "Registered".

Image from Gyazo

When you check the Subscribers page, you can see that the subscriber is registered.

Image from Gyazo

Send a delivery message

Send the following email to the subscriber from Notification Message Editor.

<p>Thank you for trying our service!</p>
<p>This email is sent from Kuroco. If you wish to unsubscribe, please click <a href="http://localhost:3000/subscribe_with_key/unsubscribe?email=%email%&key=%key%">here</a>.</p>
<p>Best regards,<br>Kuroco Team</p>

Image from Gyazo

You will receive a delivery message with a link to the unsubscribe page.

Image from Gyazo

Accessing the link will send the email and key attached as query parameters to Kuroco's endpoint, completing the unsubscribe process.

Image from Gyazo

This concludes the explanation of how to build a form to register and unsubscribe subscribers for the delivery function.


Support

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