我使用 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() 方法的文档,但我不确定如何继续。
我需要一些基本代码的帮助,以便在他们注册后提取这些数据。
感谢任何帮助,因为过去两天我一直在为此抓狂。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
无法从
onAuthStateChanged中的 OAuth 提供程序获取其他信息。您必须在登录提供商后才能获取该信息。所以在您致电 Firebase 之前。提供商的结果包含您在步骤 2 中请求的范围值:https://firebase.google.com/docs/auth/web/google-signin#handle_the_sign-in_flow_with_the_firebase_sdk