Şifre sıfırlama kodu doğrulama ile
✅ Senaryonun amacı:
- Kullanıcı bir e-posta veya telefon numarası girer
- Bir onay kodu alır (e-posta veya SMS ile)
- Kodu ve yeni şifreyi girer. Şifre OneEntry'de güncellenir
✅ Gereksinimler:
- OneEntry API ile kimlik doğrulama için geçerli bir PROJECT_URL ve APP_TOKEN.
- Kayıtlı kullanıcı
📌 Önemli:
- generateCode metodu bir kodu e-posta/SMS ile gönderir.
- Kod ile şifre değiştirme, kullanıcı yetkilendirmesi olmadan çalışır.
- 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'i 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 { AuthProvider } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
3. AuthProvider.generateCode() ile e-posta/SMS ile gönderilen kodu oluşturma ve gönderme
Veri:
your-user@email.com
Örnek:
const generateCode = await AuthProvider.generateCode(
'email',
emailReg,
'generate_code',
);
4. Kullanıcı e-posta/SMS ile aldığı kodu girer
Örnek:
const code = prompt('Doğrulama kodunu girin:');
Sonuç:
547491
5. AuthProvider.checkCode() ile kodu kontrol etme
Örnek:
const isCodeValid = await AuthProvider.checkCode(
'email',
authData[0].value,
'password_code',
code,
);
Sonuç:
true
6. AuthProvider.changePassword() ile şifreyi değiştirme
Veri:
your-user@email.com
Örnek:
if (isCodeValid) {
const changePassword = await AuthProvider.changePassword(
'email',
emailReg,
1,
code,
'123456',
);
console.log('✅ Şifre başarıyla değiştirildi', changePassword);
} else {
console.log('❌ Geçersiz doğrulama kodu');
}
Sonuç:
true
7. AuthProvider.auth() ile kullanıcı kimlik doğrulaması
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"
}
Son örnek
// 1. oneEntry'i 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';
const emailReg = 'your-user@email.com';
// 2. API istemcisi oluşturma ve AuthProvider'ı içe aktarma
const { AuthProvider } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
// 3. AuthProvider.generateCode() ile kodu oluşturma ve gönderme (e-posta/SMS)
const generateCode = await AuthProvider.generateCode(
'email',
emailReg,
'auth',
);
// 4. Kullanıcı e-posta/SMS ile aldığı kodu girer
const code = prompt('Doğrulama kodunu girin:') as string;
setCode(code);
// 5. AuthProvider.checkCode() ile kodu kontrol etme
const isCodeValid = await AuthProvider.checkCode(
'email',
authData[0].value,
'password_code',
code,
);
// 6. Kod geçerliyse AuthProvider.changePassword() ile şifreyi değiştirme
let changePassword = null;
if (isCodeValid) {
changePassword = await AuthProvider.changePassword(
'email',
emailReg,
1,
code,
'123456',
);
console.log('✅ Şifre başarıyla değiştirildi', changePassword);
} else {
console.log('❌ Geçersiz doğrulama kodu');
}
// 7. AuthProvider.auth() ile kullanıcı kimlik doğrulaması
const authData = [
{
marker: 'email_reg',
value: 'your-user@email.com',
},
{
marker: 'password_reg',
value: '123456',
},
];
const authResponse = await AuthProvider.auth('email', {
authData,
});
console.log(authResponse);