WebOTP is an important API to verify OTP on the phone web browser automatically without having to manually type your OTP.
Chrome recently release...
For further actions, you may consider blocking this person and/or reporting abuse
Hey Jyotishman, I'm using the same in my nextJS App, but when I get the pop-up and press allow, it doesn't fill my input with the OTP number; I tried a million ways to fix it, but I get Nothing. Do u know why?
Hey in that case first you will have to pick the otp from the message. Why dont u console.log your otp first from the message ?
hey I wrote
onst handleOTPAutoFill = useCallback(() => {
if ('OTPCredential' in window) {
window.addEventListener('DOMContentLoaded', e => {
const ac = new AbortController();
navigator.credentials
.get({
otp: { transport: ['sms'] },
signal: ac.signal,
} as OTPCredentialRequestOptions)
.then((otp: any) => {
if (otp && otp.code) {
console.log(otp, 'OTP in autofill');
const otpArray = otp.code.split('');
console.log(otpArray, 'OTParray');
const updatedOtpValue = otpArray.slice(0, 6);
console.log(updatedOtpValue, 'updatedOTPValue');
setOtpValue(updatedOtpValue);
handleOTPChange(updatedOtpValue);
handleVerifyOnClick();
console.log('abort after this');
ac.abort();
console.log('end');
}
})
.catch(err => {
console.error('OTP Autofill error:', err);
});
});
} else {
alert('WebOTP not supported!.');
}
}, []);
useEffect(() => {
handleOTPAutoFill();
}, [handleOTPAutoFill]);
and chrome ask for permission but nothing happens after it
it says the otp for your adhar num xxx is 648375.
what should I do @jyotishman @mhmdndri
are u trying on localhost ?
Hi Mohammad,
I am also facing the same problem, can you please help me to resolve this issue.
Hi Mohammed, was your problem resolved? If yes, then can you tell me how? I am facing the same issue. Thanks.
Yes, I solve it. The most important part is the text of the SMS. I deploy and test, and as he said in the article, it should contain the domain Address and the OTP code with #. remember you should use "navigator.credential.get " in the useEffect. I hope you can solve it, but If you can't, text me here to check your code together and solve it.
Hey can you help me with this. facing issue as it does ask for permission but doesn't paste the OTP.
What issue are u getting? @suhasini_singyan_8aa3ad76
Hi Partha,
I am also facing the same problem, can you please help me to resolve this issue.
Hey Jyotishman. I am using same for a while but it pop up everytime an sms having OTP received from different source like bank, zomatoetc.,
how can i prevent it. pop up everytime a new OTP SMS received.
I tried on button clicked but no luck.
with correct domain name
dev-to-uploads.s3.amazonaws.com/i/...
with wrong domain name.
dev-to-uploads.s3.amazonaws.com/i/...
hi, Can you send the sample code so i that i can assist u better where you might be wrong.
This is form page.
And this is script.
@oceanrational : I have one doubt on your above code why you gave
const form = input.closest('form');
form id should come instead of form. What do you think?
@jyotishman In your code implementation I have seen
const form = input.closest('form'); which form is this ?
When we are submitting form does it means after otp is autofilled it will redirect to next page
I have written below code but it's not submitting my custom form.
Do we need to give input.closest('form '); here for every case ?
Below is my code implementation for autofill otp
if ('OTPCredential' in window) {
window.addEventListener('DOMContentLoaded', e => {
const input = document.querySelector('input[autocomplete="one-time-code"]');
if (!input) return;
const ac = new AbortController();
const form = input.closest('firstInput');
if (form) {
form.addEventListener('submit', e => {
ac.abort();
});
}
navigator.credentials.get({
otp: {transport: ['sms']},
signal: ac.signal
}).then(otp => {
input.value = otp.code;
hi Jyotishman! is this api / demo still working?
i've tried your demo and isn't working for me. Im using clicksend.com to send the SMS to my device. the auth dialog never appers, thus the reading of the SMS...
thanks!
Hey Jyotishman, My input have 4 separate field how to autocomplete them any idea ? I think this will work only if we have a single input field
in that case u have to programatically fetch the otp and split the numbers into 4 digits.
or another case would be make one input and design from css that it look like 4 input boxes.