Đăng ký nội dung
✅ 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 một đề 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.
📌 Quan trọng:
- Chúng tôi không xử lý lỗi trong các ví dụ này.
- Bạn có thể xử lý lỗi trong trycatch hoặc trong một cấu trúc như "await Promise.catch((error) => error)"
Kịch bản
1. Nhập oneEntry và định nghĩa 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": 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": "Tiền mặt"
},
"products": [
{
"id": 3,
"title": "1 month",
"sku": "",
"previewImage": null,
"price": 100,
"quantity": 1
}
],
"isCompleted": false
}
],
"total": 1
}
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