问题描述
我正在做一个简单的注册。本来打算用email来创建账号的,问题是提交表单的时候,手机号也用新用户登录了。
我怎样才能防止这种情况发生?我只需要使用发送 otp 功能。
sendOTP() {
var self = this
const phoneNumber = '+' + this.phone;
const appVerifier = window.recaptchaVerifier;
firebase.auth().signInWithPhoneNumber(phoneNumber,appVerifier)
.then((confirmationResult) => {
window.confirmationResult = confirmationResult;
self.otpSent = true
}).catch((error) => {
self.errorMsg = error.code + ': ' + error.message;
});
},register() {
var self = this
const code = this.otp;
window.confirmationResult.confirm(code).then((result) => {
console.log(result)
firebase.auth().createuserWithEmailAndPassword(this.email,this.password)
.then((userCredential) => {
firebase.database().ref('users').set({
email: this.email,name: this.name,password: this.password,phone: this.phone,userCredential: userCredential
},(error) => {
if (error) {
// The write Failed...
self.errorMsg = error.message
} else {
self.$router.push('/dashboard')
}
});
})
.catch((error) => {
self.errorMsg = error.message
});
}).catch((error) => {
self.errorMsg = error.message;
});
}
解决方法
很高兴看到您尝试使用 Firebase 的移动验证,但问题在于您尝试使用 SDK,它由 Firebase 的人员编写,以某种方式执行操作。
下面的示例假设您在后端有一个 Firebase Firestore DB,但这甚至可以与 RealTime DB 一起使用。
如果您可以编写云函数后端并在 /userDetails 集合中维护用户个人资料,其中单个文档记录与 Firebase 身份验证的用户 UID 一起存储,您可以在那里存储电话号码及其布尔状态作为 phoneVerified:true 或 false。
因此 Firestore 中的集合看起来像 /userDetails/UserUID/ 字段 phoneNumber 作为 Number 和 phoneVerified:true 或 false
通过触发对云函数的 API 调用在后端执行此操作。
Then you can refer to this link too 最好的问候!!
:-)