Nhảy đến nội dung

Đăng ký nội dung

Trong ví dụ này, chúng tôi sẽ trình bày cách quản lý quyền truy cập dựa trên đăng ký vào nội dung bằng cách sử dụng API OneEntry. Chúng tôi kiểm tra xem người dùng có đăng ký hoạt động hay không và cấp quyền truy cập vào nội dung cao cấp hoặc yêu cầu họ đăng ký.

✅ Mục đích của kịch bản:

  • Chỉ những người dùng có đăng ký mới có quyền truy cập vào nội dung
  • Việc kiểm tra dựa trên Đơn hàng, Người dùng.
  • Những người không được ủy quyền hoặc chưa mua sẽ nhận được đề nghị đăng ký.

✅ Những gì bạn cần:

  • Một PROJECT_URL và APP_TOKEN hợp lệ để xác thực với API OneEntry.
  • Biểu mẫu với dấu hiệu "subscriptions", thuộc tính "expired_date" và "subscription_time" và loại "Đơn hàng"
  • Người dùng đã đăng ký

📌 Quan trọng:

  • Những ví dụ này không bao gồm xử lý lỗi.
  • Bạn có thể quản lý lỗi bằng cách sử dụng khối try-catch hoặc bằng cách sử dụng cấu trúc như await Promise.catch((error) => error).

Kịch bản

1. Nhập defineOneEntry từ SDK và xác định url và token

Ví dụ:

import { defineOneEntry } from 'oneentry';

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

2. Tạo một khách hàng API với hàm defineOneEntry()

Ví dụ:

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

3. Chuẩn bị dữ liệu cho việc xác thực người dùng

Ví dụ:

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

4. Xác thực và lấy dữ liệu người dùng AuthProvider.auth()

Ví dụ:

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

5. Chúng tôi lấy danh sách đơn hàng của người dùng với Orders.getAllOrdersByMarker()

Ví dụ:

const orders = await Orders.getAllOrdersByMarker('subscriptions');
Kết quả:
{
"items": [
{
"id": 95,
"storageId": 2,
"createdDate": "2025-08-29T13:32:04.663Z",
"statusIdentifier": "active",
"formIdentifier": "subscription",
"formData": [
{
"marker": "expired_date",
"type": "date",
"value": {
"fullDate": "2026-05-07T00:00:00.000Z",
"formattedValue": "03-05-2026 00:00",
"formatString": "DD-MM-YYYY HH:mm"
}
}
],
"attributeSetIdentifier": "subscriptions",
"totalSum": "100.00",
"currency": "",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Tiền mặt"
},
"products": [
{
"id": 3,
"title": "1 tháng",
"sku": "",
"previewImage": null,
"price": 100,
"quantity": 1
}
],
"isCompleted": false
},
{
"id": 94,
"storageId": 2,
"createdDate": "2025-08-29T13:29:30.068Z",
"statusIdentifier": "active",
"formIdentifier": "subscription",
"formData": [
{
"marker": "expired_date",
"type": "date",
"value": {
"fullDate": "2026-05-07T00:00:00.000Z",
"formattedValue": "07-05-2026 00:00",
"formatString": "DD-MM-YYYY HH:mm"
}
}
],
"attributeSetIdentifier": "subscriptions",
"totalSum": "100.00",
"currency": "",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Tiền mặt"
},
"products": [
{
"id": 3,
"title": "1 tháng",
"sku": "",
"previewImage": null,
"price": 100,
"quantity": 1
}
],
"isCompleted": false
},
{
"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 tháng",
"value": "1",
"extended": {
"type": "real",
"value": "100"
}
}
]
}
],
"attributeSetIdentifier": "subscriptions",
"totalSum": "100.00",
"currency": "",
"paymentAccountIdentifier": "cash",
"paymentAccountLocalizeInfos": {
"title": "Tiền mặt"
},
"products": [
{
"id": 3,
"title": "1 tháng",
"sku": "",
"previewImage": null,
"price": 100,
"quantity": 1
}
],
"isCompleted": false
}
],
"total": 3
}

6. Kiểm tra xem có đăng ký hoạt động hay không

Ví dụ:

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,
);
Kết quả:
true

7. Nếu đăng ký đang hoạt động, chúng tôi cấp quyền truy cập, nếu không chúng tôi tạo một đơn hàng và tạo thanh toán

Ví dụ:

let content = null;
if (hasActiveSubscription) {
// ✅ Đăng ký đang hoạt động → hiển thị nội dung
content = await Pages.getPageByUrl('premium_page');
console.log('🎉 Quyền truy cập đã được mở:', content);
} else {
// ❌ Không có đăng ký → tạo một đơn hàng và tạo thanh toán
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 tháng',
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('💸 Tiến hành thanh toán cho đăng ký của bạn:', payment)
}
Kết quả:
{
"id": 7,
"parentId": null,
"pageUrl": "premium_page",
"depth": 0,
"localizeInfos": {
"title": "Trang cao cấp",
"menuTitle": "Trang cao cấp",
"htmlContent": "",
"plainContent": ""
},
"isVisible": true,
"blocks": [],
"type": "common_page",
"templateIdentifier": null,
"attributeSetIdentifier": null,
"attributeValues": {},
"moduleFormConfigs": [],
"isSync": false
}

Ví dụ cuối cùng

// 1. Nhập defineOneEntry từ SDK và xác định PROJECT_URL và APP_TOKEN
import { defineOneEntry } from 'oneentry';

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

// 2. Tạo một khách hàng API
const { Users, Orders, Pages, Payments, AuthProvider, Forms } =
defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

// 3. Chuẩn bị dữ liệu cho việc xác thực người dùng
const form = await Forms.getFormByMarker('subscription');
// expired_date - Ngày
// subscription_time - danh sách

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

// 4. Xác thực và lấy dữ liệu người dùng
const authResponse = await AuthProvider.auth('email', {
authData,
});
const user = await Users.getUser();

// 5. Chúng tôi lấy danh sách đơn hàng của người dùng
const orders = await Orders.getAllOrdersByMarker('subscriptions');

// 6. Kiểm tra xem có đăng ký hoạt động hay không
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. Nếu đăng ký đang hoạt động, chúng tôi cấp quyền truy cập, nếu không chúng tôi tạo một đơn hàng và tạo thanh toán
let content = null;
if (hasActiveSubscription) {
// ✅ Đăng ký đang hoạt động → hiển thị nội dung
content = await Pages.getPageByUrl('premium_page');
console.log('🎉 Quyền truy cập đã được mở:', content);
} else {
// ❌ Không có đăng ký → tạo một đơn hàng và tạo thanh toán
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 tháng',
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('💸 Tiến hành thanh toán cho đăng ký của bạn:', payment);
}