Password reset with code verification
✅ Purpose of the scenario:
- The user enters an email or phone number
- Receives a confirmation code (via email or SMS)
- Enters the code and new password. Password is being updated in OneEntry
✅ What you need:
- A valid PROJECT_URL and APP_TOKEN for authentication with the OneEntry API.
- Registered user
📌 Important:
- The generateCode method sends a code to email/sms.
- Change password with code works without user authorization.
- We do not handle errors in these examples.
- You can handle errors in trycatch or in a construction like "await Promise.catch((error) => error)"
Scenario
1. Import oneEntry and define url and token
Example:
import { defineOneEntry } from 'oneentry';
const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';
2. Creating an API client with defineOneEntry() function
Example:
const { AuthProvider } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
3. Generate and send code with AuthProvider.generateCode() send by email/SMS
Data:
your-user@email.com
Example:
const generateCode = await AuthProvider.generateCode(
'email',
emailReg,
'generate_code',
);
4. The user enters the code received by email/sms
Example:
const code = prompt('Enter verification code:');
Result:
547491
5. Code review with AuthProvider.checkCode()
Example:
const isCodeValid = await AuthProvider.checkCode(
'email',
authData[0].value,
'password_code',
code,
);
Result:
true
6. Change password with AuthProvider.changePassword()
Data:
your-user@email.com
Example:
if (isCodeValid) {
const changePassword = await AuthProvider.changePassword(
'email',
emailReg,
1,
code,
'123456',
);
console.log('✅ Password successfully changed', changePassword);
} else {
console.log('❌ Invalid verification code');
}
Result:
true
7. User authentication with AuthProvider.auth()
Data:
[
{
"marker": "email_reg",
"value": "your-user@email.com"
},
{
"marker": "password_reg",
"value": "123456"
}
]
Example:
const authResponse = await AuthProvider.auth('email', {
authData,
});
Result:
{
"userIdentifier": "your-user@email.com",
"authProviderIdentifier": "email",
"accessToken": "eyJhbGciOiJIUzI1NiIsInR...pZCI6MTYsImF1dGhQ"
"refreshToken": "1745494429101-...-2834edf8"
}
Final example
// 1. Import oneEntry and define PROJECT_URL and 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. Creating an API client and import AuthProvider handle
const { AuthProvider } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
// 3. Generate and send code with AuthProvider.generateCode() (email/SMS)
const generateCode = await AuthProvider.generateCode(
'email',
emailReg,
'auth',
);
// 4. The user enters the code received by email/sms
const code = prompt('Enter verification code:') as string;
setCode(code);
// 5. Code review with AuthProvider.checkCode()
const isCodeValid = await AuthProvider.checkCode(
'email',
authData[0].value,
'password_code',
code,
);
// 6. Change password with AuthProvider.changePassword() if code is valid
let changePassword = null;
if (isCodeValid) {
changePassword = await AuthProvider.changePassword(
'email',
emailReg,
1,
code,
'123456',
);
console.log('✅ Password successfully changed', changePassword);
} else {
console.log('❌ Invalid verification code');
}
// 7. User authentication with AuthProvider.auth()
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);