我使用 Firebase 驗證透過兩種方法對網站上的使用者進行身份驗證:電子郵件/密碼和 Google OAuth 登入。
每當使用者透過電子郵件/密碼方法註冊時,我都會使用以下程式碼來保存他們在 Cloud Firestore 文件中輸入的資訊:
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
setDoc(doc(db,"accounts",user.uid), {
user_id: user.uid,
first_name: first_name,
last_name: last_name,
date_of_birth: dob,
email: email,
password: password,
date_created: date
})
})
.catch((error) => {
//pass
};
但是,當他們使用Google 登入時(我使用的是重定向方法),我不知道如何做同樣的事情:我想從他們的Google 帳戶中提取用戶資料(一般來說,只有他們的姓名、出生日期及其Gmail 地址)。我已經瀏覽了 getRedirectResult() 方法的文檔,但我不確定如何繼續。
我需要一些基本程式碼的幫助,以便在他們註冊後提取這些資料。
感謝任何幫助,因為過去兩天我一直在為此抓狂。
無法從
onAuthStateChanged中的 OAuth 提供者取得其他資訊。您必須在登入提供者後才能取得該資訊。所以在您致電 Firebase 之前。提供者的結果包含您在步驟 2 中要求的範圍值:https://firebase.google.com/docs/auth/web/google-signin#handle_the_sign-in_flow_with_the_firebase_sdk
#