Nhảy đến nội dung

Đăng ký người dùng

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

  • Chúng tôi nhận được mẫu đăng ký để tạo mẫu frontend
  • Người dùng nhập dữ liệu cần thiết và đăng ký.
  • Sau khi đăng ký, người dùng nhận mã kích hoạt qua email/sms
  • Người dùng nhập mã kích hoạt và kích hoạt người dùng đã tạo.

✅ 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.
  • Mẫu đăng ký với dấu hiệu "reg"
  • Các trường mẫu (thuộc tính) với các dấu hiệu "email_reg", "password_reg", "name_reg", "phone_reg"
  • Email/số điện thoại hợp lệ để nhận mã kích hoạt

📌 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 { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

3. Chúng tôi nhận được một mẫu đăng ký với Forms.getFormByMarker() để tạo mẫu frontend

Ví dụ:

const form = await Forms.getFormByMarker('reg');
Kết quả:
{
"id": 4,
"attributeSetId": 5,
"type": "sing_in_up",
"localizeInfos": {
"title": "Đăng ký",
"titleForSite": "",
"successMessage": "",
"unsuccessMessage": "",
"urlAddress": "",
"database": "0",
"script": "0"
},
"version": 0,
"position": 1,
"identifier": "reg",
"processingType": "script",
"templateId": null,
"attributes": [
{
"type": "string",
"marker": "email_reg",
"isLogin": true,
"isSignUp": false,
"position": 1,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {
"requiredValidator": {
"strict": true
},
"emailInspectionValidator": true
},
"localizeInfos": {
"title": "E-mail"
},
"additionalFields": [],
"isNotificationEmail": false,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "string",
"marker": "name_reg",
"isLogin": null,
"isSignUp": true,
"position": 2,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {
"requiredValidator": {
"strict": true
}
},
"localizeInfos": {
"title": "Tên"
},
"additionalFields": [],
"isNotificationEmail": false,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "string",
"marker": "phone_reg",
"isLogin": null,
"isSignUp": true,
"position": 3,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {
"requiredValidator": {
"strict": true
}
},
"localizeInfos": {
"title": "Điện thoại"
},
"additionalFields": [],
"isNotificationEmail": false,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "string",
"marker": "password_reg",
"isLogin": null,
"isSignUp": null,
"position": 4,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {
"requiredValidator": {
"strict": true
},
"stringInspectionValidator": {
"stringMax": 0,
"stringMin": 0,
"stringLength": 0
}
},
"localizeInfos": {
"title": "Mật khẩu"
},
"additionalFields": [],
"isNotificationEmail": false,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "string",
"marker": "email_notification_reg",
"isLogin": null,
"isSignUp": null,
"position": 5,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "email để thông báo"
},
"additionalFields": [],
"isNotificationEmail": true,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
}
]
}

4. Chuẩn bị dữ liệu cho việc đăng ký người dùng và đăng ký người dùng với AuthProvider.signUp()

Dữ liệu:

{
"formIdentifier": "reg",
"authData": [
{
"marker": "email_reg",
"value": "your-user@email.com"
},
{
"marker": "password_reg",
"value": "123456"
}
],
"formData": {
"en_US": [
{
"marker": "name_reg",
"type": "string",
"value": "UserName"
},
{
"marker": "phone_reg",
"type": "string",
"value": "+18007986545"
},
{
"marker": "email_notification_reg",
"type": "string",
"value": "your-user@email.com"
}
]
},
"notificationData": {
"email": "your-user@email.com",
"phonePush": [],
"phoneSMS": "+18007986545"
},
"langCode": "en_US"
}

Ví dụ:

const newUser = await AuthProvider.signUp('email', registrationData);
Kết quả:
{
"identifier": "your-user@email.com",
"isActive": false,
"formData": [
{
"marker": "name_reg",
"type": "string",
"value": "UserName"
},
{
"marker": "phone_reg",
"type": "string",
"value": "+18007986545"
},
{
"marker": "email_notification_reg",
"type": "string",
"value": "your-user@email.com"
}
],
"notificationData": {
"email": "your-user@email.com",
"phonePush": [],
"phoneSMS": "+18007986545"
},
"locale": "en_US",
"updatedDate": "2025-04-30T20:07:38.156Z",
"deletedAt": null,
"id": 33,
"version": 0,
"state": {}
}

5. Tạo mã xác nhận với AuthProvider.generateCode() gửi đến email/sms

Dữ liệu:

your-user@email.com

Ví dụ:

const generateCode = await AuthProvider.generateCode('email', emailReg, 'auth');
Kết quả:
{
"statusCode": 400,
"timestamp": "2025-04-30T20:07:39.755Z",
"message": "Người dùng đã có mã",
"pageData": null
}

6. Người dùng nhập mã → xác minh

Ví dụ:

const code = prompt('Nhập mã xác minh:') as string;

7. Kiểm tra mã với AuthProvider.checkCode() Nếu mã đúng thì kích hoạt người dùng

Ví dụ:

const isCorrect = await AuthProvider.checkCode(
'email',
emailReg,
'1',
code,
);
if (isCorrect) {
await AuthProvider.activateUser('email', emailReg, code);
console.log('✅ Người dùng đã được kích hoạt!');
} else {
console.log('❌ Mã xác minh không hợp lệ');
}

8. Xác thực người dùng với 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"
}

Ví dụ cuối cùng

// 1. Nhập oneEntry và định nghĩa PROJECT_URL và APP_TOKEN
import { defineOneEntry } from 'oneentry';

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

const emailReg = 'your-user@email.com';

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

// 3. Chúng tôi nhận được một mẫu đăng ký với các trường tùy chỉnh để tạo mẫu frontend
const form = await Forms.getFormByMarker('reg');

// 4. Chuẩn bị dữ liệu cho việc đăng ký người dùng và đăng ký người dùng
const authData = [
{
marker: 'email_reg',
value: 'your-user@email.com',
},
{
marker: 'password_reg',
value: '123456',
},
];
const formData = [
{
marker: 'name_reg',
type: 'string',
value: 'UserName',
},
{
marker: 'phone_reg',
type: 'string',
value: '+18007986545',
},
{
marker: 'email_notification_reg',
type: 'string',
value: emailReg,
},
];
const notificationData = {
email: emailReg,
phonePush: [],
phoneSMS: '+18007986545',
};
const registrationData = {
formIdentifier: 'reg',
authData: authData,
formData: formData,
notificationData: notificationData,
};
let newUser = await AuthProvider.signUp('email', registrationData);

// 5. Tạo mã xác nhận (gửi đến email)
let generateCode = await AuthProvider.generateCode('email', emailReg, 'auth');

// 6. Người dùng nhập mã → xác minh
const code = prompt('Nhập mã xác minh:') as string;

// 7. Kích hoạt người dùng
const isCorrect = await AuthProvider.checkCode(
'email',
emailReg,
'1',
code,
);

if (isCorrect) {
await AuthProvider.activateUser('email', emailReg, code);
console.log('✅ Người dùng đã được kích hoạt!');
} else {
console.log('❌ Mã xác minh không hợp lệ');
}

// 8. Xác thực người dùng
const authResponse = await AuthProvider.auth('email', {
authData,
});
console.log(authResponse);