[英]How to know if the email has already been registered in firebase authentication, javascript?
[英]How to know if a user has just been created or already existed with google signin and firebase web 9?
我在使用 web 版本 9 的 firebase 身份驗證時遇到問題,使用 Google 登錄時,新創建的用戶(注冊)和之前創建的用戶(登錄)之間沒有明顯區別,兩者他們使用相同的代碼庫:
import { getAuth, GoogleAuthProvider } from "firebase/auth"
const auth = getAuth()
const googleProvider = new GoogleAuthProvider()
signInWithPopup(auth, googleProvider).then((result) => {
const user = result.user
})
我想區分新用戶或現有用戶,以確保在創建用戶后立即向新用戶詢問更多信息。
我找到了一些解決方案,您可以使用 firestore 並檢查用戶是否在數據庫中,但我認為應該有一個更簡單直接的解決方案。
我在 stackoverflow 上找到的其他解決方案適用於比版本 9 更舊的版本。
您可以使用getAdditionalUserInfo
來檢查用戶是否是新用戶,如下所示:
import { getAuth, GoogleAuthProvider, getAdditionalUserInfo } from "firebase/auth"
const auth = getAuth()
const googleProvider = new GoogleAuthProvider()
signInWithPopup(auth, googleProvider).then((result) => {
const additionalUserInfo = getAdditionalUserInfo(result)
// you can know if the user is new or not
cons isNewUser = additionalUserInfo.isNewUser
const user = result.user
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.