A React Native module for generating One-Time Password (OTP) codes using the Time-Based One-Time Password (TOTP) algorithm.
Install the package from npm:
npm install react-native-otp-code-generatorLink the native module using React Native's CLI:
react-native link react-native-otp-code-generatorImport the methods in your JavaScript file:
import { generateOtp, generateOtpWithTime, } from 'react-native-otp-code-generator';To generate a 6-digit OTP code, you can use the generateOtp function with callbacks:
generateOtp(
'base32SecretKey',
(otpcode) => {
// Use the generated OTP code
console.log('Generated OTP:', otpcode);
},
(errorcode, errorMessage) => {
console.error(`Error (${errorCode}): ${errorMessage}`);
}
);To generate a 6-digit OTP code along with the remaining time percentage until the next code is generated, use the generateOtpWithTime function with callbacks:
generateOtpWithTime(
'base32SecretKey',
(otpData) => {
const { otpcode, timeRemaining } = otpData;
// Use the generated OTP code and time remaining
console.log('Generated OTP:', otpcode);
console.log('Time remaining:', timeRemaining);
},
(errorcode, errorMessage) => {
console.error(`Error (${errorCode}): ${errorMessage}`);
}
);If the provided base32 secret key is invalid or any other error occurs during the OTP code generation process, the error callback will be invoked with an error message. Handle errors using the error callback as shown in the examples above.
Please note that this README assumes you have already set up a React Native project and are familiar with the basic concepts of React Native development. If you encounter any issues during installation or usage, refer to the official React Native documentation or open an issue on the library's GitHub repository.
Happy OTP Code Generation! 🚀