首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >离子防火墙手机认证短信代码已经过期。请重新发送验证代码再试一次。

离子防火墙手机认证短信代码已经过期。请重新发送验证代码再试一次。
EN

Stack Overflow用户
提问于 2020-04-08 13:08:08
回答 1查看 2.4K关注 0票数 2

我正在使用这个插件进行电话身份验证.https://ionicframework.com/docs/native/firebase-authentication

我已经成功地将顶部发送到移动号码,但每次检索时都会收到以下错误。

短信代码已经过期。请重新发送验证代码再试一次。

我还想自动验证otp(不允许用户手动输入otp)。

我认为这两个问题是相互关联的。

这是我的密码

代码语言:javascript
复制
import { AngularFireAuth } from '@angular/fire/auth';
import * as firebase from 'firebase/app';
import { FirebaseAuthentication } from '@ionic-native/firebase-authentication/ngx';

constructor(private router: Router,private fireAuth: AngularFireAuth,public firebaseAuthentication : FirebaseAuthentication) {}

send(){


      this.firebaseAuthentication.verifyPhoneNumber("+91xxxxxxxxxx", 30000).then(credential => {
        alert("code sent")

        console.log(credential)

        if(credential) {

          this.verificationid = credential

          const smsCode = prompt("Enter SMS verification code");

          let cred = firebase.auth.PhoneAuthProvider.credential(this.verificationid,smsCode)

          this.fireAuth.signInWithCredential(cred).then(val => {
            console.log(val)
            console.log("successs")
          }).catch(err => console.log(err))

        }
      })
    }
EN

回答 1

Stack Overflow用户

发布于 2020-04-11 13:43:05

结果很简单。

所有最新的android手机都支持自动验证OTP。verifyPhoneNumber方法自动验证otp。因此,当我们要求用户输入OTP时,它会给出一个错误“代码过期”。

因此,解决方案是在安卓设备上,听onAuthStateChanged方法,在成功的电话身份验证之后重定向用户,对于旧设备或ios进入otp手动工作。

这是完整的代码

html

代码语言:javascript
复制
<div [hidden]="display_otp">

  <ion-item>
    <ion-label position="floating"> Enter your mobile number </ion-label>
    <ion-input type="tel" [formControl] = "mobile_no" ></ion-input>

  </ion-item>
  <ion-button [disabled] = "mobile_no.invalid" (click)="Send(mobile_no.value)">Continue</ion-button>
</div>

<div *ngIf="display_otp">
  <ion-item>
    <ion-label position="floating"> Enter your OTP  </ion-label>
    <ion-input type="tel" [formControl] = "otp" ></ion-input>

  </ion-item>

  <ion-button [disabled] = "otp.invalid" (click)="enter_otp(otp.value)">Submit</ion-button>
</div>

.ts

代码语言:javascript
复制
constructor(){


this.firebaseAuthentication.onAuthStateChanged().subscribe(user =>{

        if(user) {
          console.log(user)

          console.log("success")
          // OTP verifired. Do success operation
        }
        else {
          console.log("state not changed")
       // wrong otp
        }
      })

}


    Send(mobile_no){
        console.log(mobile_no) 
        let mobile ="+91" + mobile_no
        console.log(mobile)


        this.firebaseAuthentication.verifyPhoneNumber(mobile, 30000).then(credential =>{
          if(credential){

            console.log(credential)
            this.verificationid = credential
            this.display_otp = true

          }
        })


       }  

       enter_otp(otp){
         console.log(otp)

         this.firebaseAuthentication.signInWithVerificationId(this.verificationid, otp).then(user =>{
          if(user) {
            console.log(user)

          }
          else {
            console.log("no user")
          }
        })

       }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61101452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档