首页 > 解决方案 > 无需注册的 iOS Firebase OTP 验证

问题描述

我需要帮助使用 Firebase 验证 OTP。我设法收到带有 OTP 的短信,但是当我验证它时,我会自动注册,而且我只知道我注册后 OTP 是否有效 - 否则我会收到类似“无效 otp”的弹出窗口。如何手动验证 otp?我的目标是打开另一个屏幕,用户可以在其中输入更多信息。

func verifyCode(){
    let credential = PhoneAuthProvider.provider().credential(withVerificationID: self.CODE, verificationCode: code)
    print(credential)
    loading = true
    //here i just want to verify my OTP without signing in...
    Auth.auth().signIn(with: credential) { (result, err) in //here i am signing in...
        self.loading = false
        if let error = err{
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.error)
            self.code = ""
            self.errorMsg = error.localizedDescription
            withAnimation{ self.error.toggle()}
            return
        }
        self.gotoRegistration = true
        withAnimation{self.status = true}
    }
}

标签: iosswiftfirebasefirebase-authentication

解决方案


如果不自动登录用户,就无法使用 Firebase 身份验证的电话/OTP 提供商。

但用户登录 Firebase 的事实并不意味着您必须授予他们访问您应用中所有部分/数据的权限。如果您希望他们提供更多信息,您可以在他们登录 Firebase 之前或之后这样做,并就用户而言,使其成为同一注册流程的一部分。

所以像:

// Sign the user in with Firebase
  // Check if the user has provider the additional registration information
    // If not, send them to the registration information screen
    // If so, send them to the next screen of the app

您还可以在后端代码中或(如果您使用 Firebase 的后端服务之一)在服务器端安全规则中强制执行这些规则


推荐阅读