![](/img/trans.png)
[英]Password is gone when logging in a firebase account that was created using Email and Password
[英]Firebase - check if user created with Google Account is signing up or logging in?
我正在嘗試創建一個 Web 應用程序,您可以在其中登錄並使用 google 帳戶注冊一個帳戶。 我已經設法做到了,因此他們可以使用 signInWithPopup(provider) 登錄,但不確定如何實現注冊。 有什么建議? 或者知道我可以使用的firebase中的任何功能嗎?
使用 Google 登錄或任何其他提供商時,沒有任何單獨的login
和sign up
方法。 如果存在具有該憑據的用戶,則該用戶將登錄,否則將創建一個新用戶帳戶。 然后,您可以使用從signInWithPopUp
方法收到的結果中的additionalUserInfo.isNewUser
屬性來檢查用戶是否是新用戶。
firebase.auth().signInWithPopup(provider).then(function (result) {
const {additionalUserInfo: {isNewUser}} = result;
console.log(isNewUser ? "This user just registered" : "Existing User")
})
對於新的 Modular SDK ( V9.0.0+
),同樣可以寫成:
import { signInWithPopup, getAdditionalUserInfo } from "firebase/auth"
const result = await signInWithPopup(auth, googleProvider);
// Pass the UserCredential
const { isNewUser } = getAdditionalUserInfo(result)
到目前為止,據我了解,您有兩種登錄網站的選擇:一種是在您的網站上創建一個本地用戶名/密碼帳戶,另一種選擇是使用您的谷歌帳戶。 我想最好的方法是檢查 Google 帳戶是否使用additionalUserInfo.isNewUser
鏈接到任何現有用戶,然后按照這篇文章將您的 google 帳戶與通過您的網站在本地創建的帳戶鏈接起來:https:// firebase.google.com/docs/auth/web/account-linking?hl=lt
一旦您的應用程序中有 Firebase 依賴項。 您可以使用 createUserWithEmailAndPassword 方法來做到這一點。
firebase
.auth()
.createUserWithEmailAndPassword("email@domain.com", "123123")
.then(data => {
data.user.updateProfile({
displayName: this.form.name
}).then(() => {});
})
.catch(err => {
this.error = err.message;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.