Reputation: 109
I am trying the autofill functionality using the Web OTP API. In the mobile, while testing it out I get a pop up to allow to read the OTP, however after allowing it doesnt fill the OTP field. In the console I get request timed out error. Does somebody has any idea on this. It would be really helpful. Below is the code which I have written(react app ) :-
console.log("otp credential in window");
//window.addEventListener('DOMContentLoaded', e => {
const input = document.querySelector('input[autocomplete="one-time-code"]');
console.log("input is ", input);
if (!input) return;
const ac = new AbortController();
console.log("before form");
const form = input.closest('form');
if (form) {
console.log("inside if form ");
form.addEventListener('submit', e => {
ac.abort();
});
}
navigator.credentials.get({
otp: { transport:['sms'] },
signal: ac.signal
}).then(otp => {
console.log("promise resolved");
input.value = otp.code;
if (form) form.submit();
}).catch(err => {
console.log(err);
});
// });
}
Upvotes: 2
Views: 2427
Reputation: 392
You have to follow the convention of the OTP, you have been getting that wrong, hence the API is unable to read the OTP.
The correct format is:
Upvotes: 3