Skip to main content

Subscribe to content

✅ Purpose of the scenario:

  • Only users with a subscription have access to the content
  • The check is based on Orders, User.
  • Those who are not authorized or have not purchased will receive an offer to subscribe.

✅ What you need:

  • A valid PROJECT_URL and APP_TOKEN for authentication with the OneEntry API.

📌 Important:

  • We do not handle errors in these examples.
  • You can handle errors in trycatch or in a construction like "await Promise.catch((error) => error)"

Scenario

1. Import oneEntry and define url and token

Example:

import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

2. Creating an API client with defineOneEntry() function

Example:

const { Users, Orders, Pages, Payments, AuthProvider, Forms } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

3. Preparing data for user authorization

Example:

[
{
"marker": "email_reg",
"value": "your-user@email.com"
},
{
"marker": "password_reg",
"value": "123456"
}
]

4. Authorization and obtaining user data AuthProvider.auth()

Example:

const authResponse = await AuthProvider.auth('email', {
authData,
});
Result:
{
"userIdentifier": "your-user@email.com",
"authProviderIdentifier": "email",
"accessToken": "eyJhbGciOiJIUzI1NiIsInR...pZCI6MTYsImF1dGhQ"
"refreshToken": "1745494429101-...-2834edf8"
}

5. We get a list of user orders with Orders.getAllOrdersByMarker()

Example:

const orders = await Orders.getAllOrdersByMarker('subscriptions');
Result:
{
"items": [
{
"id": 91,
"storageId": 2,
"createdDate": "2025-04-30T21:48:40.628Z",
"statusIdentifier": "active",
"formIdentifier": "subscription",
"formData": [
{
"type": "date",
"marker": "expired_date",
"value": {
"fullDate": "2025-05-07T00:00:00.000Z",
"formattedValue": "07-05-2025 00:00",
"formatString": "DD-MM-YYYY HH:mm"
}
},
{
"type": "list",
"marker": "subscription_time",
"value": [
{
"title": "1 month",
"value": "1",
"extended": {
"type": "real",
"value": "100"
}
}
]
}
],
"attributeSetIdentifier": "subscriptions",
"totalSum": "100.00",
"currency": "",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Cash"
},
"products": [
{
"id": 3,
"title": "1 month",
"sku": "",
"previewImage": null,
"price": 100,
"quantity": 1
}
],
"isCompleted": false
}
],
"total": 1
}

6. Checking for an active subscription

Example:

const now = new Date();
const hasActiveSubscription = orders.items?.some(
(order: any) =>
new Date(
order.formData.find(
(d: { marker: string }) => d.marker === 'expired_date',
).value.fullDate,
) > now,
);
Result:
true

7. If the subscription is active, we provide access, otherwise we create an order and generate payment

Example:

let content = null;
if (hasActiveSubscription) {
// ✅ Subscription active → show content
content = await Pages.getPageByUrl('premium_page');
console.log('🎉 Доступ открыт:', content);
} else {
// ❌ No subscription → create an order and generate payment
const body = {
formIdentifier: 'subscription',
paymentAccountIdentifier: 'cash',
formData: [
{
type: 'date',
marker: 'expired_date',
value: {
fullDate: '2025-05-07T00:00:00.000Z',
formattedValue: '07-05-2025 00:00',
formatString: 'DD-MM-YYYY HH:mm',
},
},
{
type: 'list',
marker: 'subscription_time',
value: [
{
title: '1 month',
value: '1',
extended: {
type: 'real',
value: '100',
},
},
],
},
],
products: [
{
productId: 3,
quantity: 1,
},
],
};
const order = await Orders.createOrder('subscriptions', body);
const payment = await Payments.createSession(order.id, 'session', false);
// console.log('💸 Proceed to pay for your subscription:', payment)
}
Result:
{
"id": 7,
"parentId": null,
"pageUrl": "premium_page",
"depth": 0,
"localizeInfos": {
"title": "Premium page",
"menuTitle": "Premium page",
"htmlContent": "",
"plainContent": ""
},
"isVisible": true,
"forms": [],
"blocks": [],
"type": "common_page",
"templateIdentifier": null,
"attributeSetIdentifier": null,
"attributeValues": {},
"isSync": false
}

Final example

// 1. Import oneEntry and define PROJECT_URL and APP_TOKEN
import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

// 2. Creating an API client
const { Users, Orders, Pages, Payments, AuthProvider, Forms } =
defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

// 3. Preparing data for user authorization
// const form = await Forms.getFormByMarker('subscription');
// expired_date - Date
// subscription_time - list

const authData = [
{
marker: 'email_reg',
value: 'your-user@email.com',
},
{
marker: 'password_reg',
value: '123456',
},
];

// 4. Authorization and obtaining user data
const authResponse = await AuthProvider.auth('email', {
authData,
});
const user = await Users.getUser();

// 5. We get a list of user orders
const orders = await Orders.getAllOrdersByMarker('subscriptions');

// 6. Checking for an active subscription
const now = new Date();
const hasActiveSubscription = orders.items?.some(
(order: any) =>
new Date(
order.formData.find(
(d: { marker: string }) => d.marker === 'expired_date',
).value.fullDate,
) > now,
);

// 7. If the subscription is active, we provide access, otherwise we create an order and generate payment
let content = null;
if (hasActiveSubscription) {
// ✅ Subscription active → show content
content = await Pages.getPageByUrl('premium_page');
console.log('🎉 Access is open:', content);
} else {
// ❌ No subscription → create an order and generate payment
const body = {
formIdentifier: 'subscription',
paymentAccountIdentifier: 'cash',
formData: [
{
type: 'date',
marker: 'expired_date',
value: {
fullDate: '2025-05-07T00:00:00.000Z',
formattedValue: '07-05-2025 00:00',
formatString: 'DD-MM-YYYY HH:mm',
},
},
{
type: 'list',
marker: 'subscription_time',
value: [
{
title: '1 month',
value: '1',
extended: {
type: 'real',
value: '100',
},
},
],
},
],
products: [
{
productId: 3,
quantity: 1,
},
],
};
const order = await Orders.createOrder('subscriptions', body);
const payment = await Payments.createSession(order.id, 'session', false);
console.log('💸 Proceed to pay for your subscription:', payment);
}