最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - auto read SMSOTP in react PWA - Stack Overflow

programmeradmin2浏览0评论

I am working on a login process using OTP for a PWA built-in using react.

I want to auto-fill my OTP. I was wondering if anyone knew something that could lead me in the right direction about this.

I am working on a login process using OTP for a PWA built-in using react.

I want to auto-fill my OTP. I was wondering if anyone knew something that could lead me in the right direction about this.

Share Improve this question asked May 26, 2020 at 20:12 GauravGaurav 951 gold badge1 silver badge8 bronze badges 2
  • Are you using React Native? – Flink Commented May 26, 2020 at 20:23
  • No, I am creating PWA using react js. – Gaurav Commented May 26, 2020 at 21:23
Add a comment  | 

3 Answers 3

Reset to default 10

There is a draft specification for a Web OTP API.

The Web OTP API lets your app receive specially-formatted messages bound to your app's origin. From this, you can programmatically obtain an OTP from an SMS message and verify a phone number for the user more easily.

https://web.dev/web-otp/

Just to be sure sms should be in correct format. Below is the code which i put into componentDidMount and it works.

import "./styles.css";

import React from "react";

export default class App extends React.Component {
  state = {
    otp: ""
  };

componentDidMount() {

if ("OTPCredential" in window) {
  const ac = new AbortController();

  navigator.credentials
    .get({
      otp: { transport: ["sms"] },
      signal: ac.signal
    })
    .then((otp) => {
      this.setState({ otp: otp.code });
      ac.abort();
    })
    .catch((err) => {
      ac.abort();
      console.log(err);
    });
}


}

  render() {
    return (
      <div className="App">
        <h1>Hello CodeSandbox</h1>
        <h2>Your OTP is: {this.state.otp}</h2>
      </div>
    );
  }
}

It always prompt whenever a sms received.

And will display otp on screen.

Open the CodeSandbox link in chrome mobile browser and send the below sms to your device

Hi this is a login otp: 9299
@t0hj4.csb.app #9299

Yes, this is possible now. Chrome release this feature in version 84 and above. With the help of WEBOTP API, we can detect OTP on the web for mobile devices.

Here is the example of Angular PWA: Here is a Web-OTP integrated code with Angular PWA Apps: https://github.com/Rohit3230/webOtpAutoReadByAngular

Go for live working URL for angular PWA app. https://rohit3230.github.io/webOtpAutoReadByAngular/

发布评论

评论列表(0)

  1. 暂无评论