如何在firebase.auth中更新用于auth的用户电话号码
Firebase提供方法:
updatePhoneNumber(phoneCredential)
但我们需要提供 phoneCredential 。 此凭证采用对象:
interface AuthCredential {
providerId: string;
signInMethod: string;
}
phoneCredential 必须包含哪些内容,我尝试发送
providerId: 'phone';
signInMethod: 'phone';
这不起作用;(
答案 0 :(得分:4)
使用当前用户/登录帐户来更新phoneNumber的解决方案。万一这对任何人都有帮助。
问题: 假设您有一个具有任意数量的单个/合并帐户的当前登录帐户,并且他们想更新其编号。他们会怎么做。
解决方案: 下面是Auth for Web / JS方法,等效于您的目标平台。
function updateProfilePhoneNumber() {
//country code plus your phone number excluding leading 0 if exists.
var phoneNumber = "+447xxxxxxxxx"; //you could provide a prompt/modal or other field in your UX to replace this phone number.
// let phoneNumber = "+441234567890"; //testing number, ideally you should set this in your firebase auth settings
// var verificationCode = "123456";
// Turn off phone auth app verification.
// firebase.auth().settings.appVerificationDisabledForTesting = true;
// This will render a fake reCAPTCHA as appVerificationDisabledForTesting is true.
// This will resolve after rendering without app verification.
var appVerifier = new firebase.auth.RecaptchaVerifier(
"recaptcha-container",
{
size: "invisible"
}
);
var provider = new firebase.auth.PhoneAuthProvider();
provider.verifyPhoneNumber(phoneNumber, appVerifier)
.then(function (verificationId) {
var verificationCode = window.prompt('Please enter the verification ' +
'code that was sent to your mobile device.');
phoneCredential = firebase.auth.PhoneAuthProvider.credential(verificationId, verificationCode);
user.currentUser.updatePhoneNumber(phoneCredential);
})
.then((result) => {
// Phone credential now linked to current user.
// User now can sign in with new phone upon logging out.
console.log(result);
})
.catch((error) => {
// Error occurred.
console.log(error);
});
}
很高兴在这里得到纠正。
答案 1 :(得分:1)
已编辑:我编辑了之前的答案,因为我现在认为它远非正确。经过一番研究后,我相信您需要使用内置的PhoneAuthProvider来更新用户电话号码,如下所示:
const credential = firebase.auth.PhoneAuthProvider.credential( verificationId, verificationCode);
user.updatePhoneNumber(credential);
原创(错误):根据documentation,您需要传递一个带有方法的对象,该对象将返回providerId,signInMethod({ {3}}是电话,如在您的代码中)和smsCode(按照should解释发送到用户移动设备的短信验证码)。尝试这样:
updatePhoneNumber({
getProvider: () => { return providerStringCode; },
getSignInMethod: () => { return signInMethodStringCode; },
getSmsCode: () => { return smsStringCode; },
});
答案 2 :(得分:1)
您需要从firebase.auth.PhoneAuthProvider.credential
获取凭据。
// Send verification code to user's phone
firebase.auth.PhoneAuthProvider.verifyPhoneNumber(phoneNumber, recaptchVerifier).then(function(verificationId) {
// verification code from user
var cred = firebase.auth.PhoneAuthProvider.credential(verificationId, verificationCode);
});
答案 3 :(得分:0)
我也有同样的问题, 在我的html页面中(使用ionic 4 modal),我有2个按钮发送OTP和验证。 所以这是我的解决方案
<ion-header>
<ion-toolbar>
<ion-title>Edit Phone</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-card>
<form [formGroup]="phoneForm">
<ion-item>
<ion-label position="stacked" >Phone :</ion-label>
<ion-input
formControlName="phoneNumber"
type="tel"
required="true"
[class.invalid]="!phoneForm.controls['phoneNumber'].valid && phoneForm.controls['phoneNumber'].touched">
</ion-input>
</ion-item>
<ion-item
class="error-message"
*ngIf="!phoneForm.controls['phoneNumber'].valid && phoneForm.controls['phoneNumber'].touched">
<ion-label>Phone Number.</ion-label>
</ion-item>
<div id="recaptcha-container" visibility="hidden"></div>
<ion-button id="sign-in-button" *ngIf="!btnSend" (click)="editPhone(phoneForm)" expand="block" [disabled]="!phoneForm.valid" data-badge="inline">SMS OTP</ion-button>
<ion-button id="sign-in-button" *ngIf="btnSend" expand="block" [disabled]="btnSend" data-badge="inline">{{timeLeft}}</ion-button>
</form>
</ion-card>
<ion-card>
<form [formGroup]="verificationForm">
<ion-item>
<ion-label position="stacked">
OTP :
</ion-label>
<ion-input formControlName="verificationCode" type="tel" >
</ion-input>
</ion-item>
<ion-button (click)="verifyPhoneCode()" expand="block" [disabled]="!verificationForm.valid||!verificationSend">
Verifikasi
</ion-button>
</form>
</ion-card>
</ion-content>
sendOTP(){
firebase.auth().currentUser.reauthenticateWithPhoneNumber(this.telepon,this.recaptchaVerifier)
.then(result=>{
this.confirmationResult = result;//firebase.auth.ConfirmationResult
})
.catch(error=>{
this.presentErrorMessage(error.message);
});
}
verifyPhoneNumber(){
let phoneProvider = new firebase.auth.PhoneAuthProvider();
phoneProvider.verifyPhoneNumber(this.telepon, this.recaptchaVerifier)//this.telepon is your new phone number
.then(verificationId=>{
let phoneCredential = firebase.auth.PhoneAuthProvider.credential(verificationId, this.verificationForm.value.verificationCode);
firebase.auth().currentUser.updatePhoneNumber(phoneCredential)
.then(()=>{
console.log("Success update phone");
this.authService.updatePhone(this.telepon).then(()=>{ //update my user database
this.dismiss(); //I'm using modal dialog for input
});
});
})
}
答案 4 :(得分:0)
如何向现有的已登录用户(cd '.\semester 7\'
)添加电话号码:
verifyPhoneNumber
通过电话号码(const appVerifier = new firebase.auth.RecaptchaVerifier('save-user-button', { size: 'invisible' }) // An element with id="save-user-button" must exist
const authProvider = new firebase.auth.PhoneAuthProvider()
const verificationId = await authProvider.verifyPhoneNumber(phoneNumber, appVerifier)
const verificationCode = window.prompt('Please enter the verification code that was sent to your phone.')
const phoneCredential = firebase.auth.PhoneAuthProvider.credential(verificationId, verificationCode)
myFirebaseUser.updatePhoneNumber(phoneCredential)
)注册/创建新用户:
signInWithPhoneNumber