How to auto verify OTP on the web using the new Web OTP API?

jyotishman

Jyotishman Saikia

Posted on October 15, 2020

How to auto verify OTP on the web using the new Web OTP API?

Alt Text

WebOTP is an important API to verify OTP on the phone web browser automatically without having to manually type your OTP.
Chrome recently released this feature and it will work only with version 84 or above.

So, this tutorial will explain to you step by step how to implement WebOTP with javascript-

  1. The first step before starting is we should know the SMS rules for the API to work correctly.
    A valid verification message might look like the following:
    Your OTP is: 1598
    @www.amazon.com #1598
    Here @www.amazon.com is the domain of the OTP verification page and #1598 is the OTP.

  2. if ('OTPCredential' in window) {
    window.addEventListener('DOMContentLoaded', e => {
    const ac = new AbortController();
    navigator.credentials.get({
    otp: { transport:['sms'] },
    signal: ac.signal
    }).then(otp => {
    alert(otp.code)
    }).catch(err => {
    console.log(err)
    });
    })
    } else {
    alert('WebOTP not supported!.')
    }

    Demo link- https://jyotishman.github.io/webOTPAPI/

💖 💪 🙅 🚩
jyotishman
Jyotishman Saikia

Posted on October 15, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related