[英]How can I set a user's display name and email with Firebase signInWithCredentials()
我正在為我的React Native
應用程序編寫登錄/注冊頁面,並且我正在使用 Firebase 的signInWithCredential()
函數。
正如您在下面的代碼中看到的那樣,我傳遞了一個憑據,如果響應顯示它是一個新用戶,我將附加到我的數據庫中,否則我將它們發送到主屏幕。
await firebase
.auth()
.signInWithCredential(credential)
.then((response) => {
if (response.additionalUserInfo.isNewUser) {
setShowVerifier(false);
setShowRegistration(true);
// append data to database if user is new
const data = { firstName, lastName, email, phoneNumber };
db.collection("users")
.doc(response.user.uid)
.set(data)
.then(() => {
setShowPhoneNumber(true);
setShowVerifier(false);
setShowRegistration(false);
navigation.navigate("Home");
}).catch(() => {
alert("Error creating account, try again");
});
} else {
setShowVerifier(false);
setShowPhoneNumber(true);
// set verification code to empty string
setVerificationCode("");
navigation.navigate("Home");
}
});
當我發現該用戶實際上是新用戶時,我將如何更新該用戶的 Firebase 身份驗證信息,例如他們的 displayName 和電子郵件? (在 JSON 響應中顯示如下)
"user": Object {
"displayName": null,
"email": null,
"emailVerified": false,
"isAnonymous": false,
"lastLoginAt": "1603914278694",
"photoURL": null,
}
將此代碼塊添加到上面找到的代碼塊中。
auth.currentUser.updateProfile({
displayName: firstName + " " + lastName,
email: email,
});
更新的代碼塊:
await firebase
.auth()
.signInWithCredential(credential)
.then((response) => {
if (response.additionalUserInfo.isNewUser) {
setShowVerifier(false);
setShowRegistration(true);
// append data to database if user is new
const data = { firstName, lastName, email, phoneNumber };
db.collection("users")
.doc(response.user.uid)
.set(data)
.then(() => {
setShowPhoneNumber(true);
setShowVerifier(false);
setShowRegistration(false);
navigation.navigate("Home");
}).catch(() => {
alert("Error creating account, try again");
});
} else {
setShowVerifier(false);
setShowPhoneNumber(true);
// set verification code to empty string
setVerificationCode("");
navigation.navigate("Home");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.