Ana içeriğe geç

Kullanıcı özelliklerini güncelleme, örneğin kullanıcı telefon numarası

✅ Senaryonun amacı:

  • Kullanıcı, bir giriş ve şifre ile kimlik doğrulaması yapar
  • Telefon alanına yeni bir telefon numarası girer
  • Kullanıcı verilerini OneEntry cms'de kaydeder

✅ Gerekenler:

  • OneEntry API ile kimlik doğrulaması için geçerli bir PROJECT_URL ve APP_TOKEN.
  • "reg" işaretine sahip kayıt formu
  • "email_reg", "password_reg", "name_reg", "phone_reg" işaretlerine sahip form alanları (özellikler)
  • Aktivasyon kodunu almak için geçerli bir e-posta/telefon numarası

📌 Önemli:

  • Bu örneklerde hataları ele almıyoruz.
  • Hataları trycatch içinde veya "await Promise.catch((error) => error)" gibi bir yapı ile ele alabilirsiniz.

Senaryo

1. oneEntry'yi içe aktarın ve url ile token'ı tanımlayın

Örnek:

import { defineOneEntry } from 'oneentry';

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

2. defineOneEntry() fonksiyonu ile bir API istemcisi oluşturma

Örnek:

const { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

3. authData oluşturma ve kullanıcıyı AuthProvider.auth() ile kimlik doğrulama

Veri:

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

Örnek:

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

4. Users.getUser() ile kullanıcıyı alarak frontend formunu oluşturma

Örnek:

const user = await Users.getUser();
Sonuç:
{
"id": 33,
"identifier": "your-user@email.com",
"authProviderIdentifier": "email",
"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"
}
],
"formIdentifier": "reg",
"groups": [],
"state": {}
}

5. Users.updateUser() ile kullanıcıyı güncelleme

Veri:

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

Örnek:

const updateUser = await Users.updateUser(body);
Sonuç:
true

6. Users.getUser() ile kullanıcıyı alma

Örnek:

const userData = await Users.getUser();
Sonuç:
{
"id": 33,
"identifier": "your-user@email.com",
"authProviderIdentifier": "email",
"formData": [
{
"marker": "name_reg",
"type": "string",
"value": "UserName"
},
{
"marker": "email_notification_reg",
"type": "string",
"value": "your-user@email.com"
},
{
"marker": "phone_reg",
"type": "string",
"value": "+19258382551"
}
],
"formIdentifier": "reg",
"groups": [],
"state": {}
}

Son örnek

// 1. oneEntry'yi içe aktarın ve PROJECT_URL ile APP_TOKEN'ı tanımlayın
import { defineOneEntry } from 'oneentry';

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

// 2. Bir API istemcisi oluşturma:
const { AuthProvider, Users } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

// 3. authData oluşturma ve kullanıcıyı AuthProvider.auth() ile kimlik doğrulama
const emailReg = 'your-user@email.com';
const authData = [
{
marker: 'email_reg',
value: emailReg,
},
{
marker: 'password_reg',
value: '123456',
},
];
const authResponse = await AuthProvider.auth('email', {
authData,
});

// 4. Kullanıcıyı almak için Users.getUser() ile frontend formunu oluşturma
const user = await Users.getUser();

// 5. Kullanıcıyı Users.updateUser() ile güncelleme
const newPhone = '+79258382551';
const body = {
formIdentifier: 'reg',
authData: [
{
marker: 'password_reg',
value: '123456',
},
],
formData: [
...user.formData.filter(
(d: { marker: string }) => d.marker !== 'phone_reg',
),
{
marker: 'phone_reg',
type: 'string',
value: newPhone,
},
],
notificationData: {
email: emailReg,
phonePush: [newPhone],
phoneSMS: newPhone,
},
};
const updateUser = await Users.updateUser(body);
console.log(updateUser);

// 6. Kullanıcıyı Users.getUser() ile alma
const userData = await Users.getUser();
console.log(userData);