使用getRedirectResult方法,如何提取Google OAuth用戶資料並將其儲存在Firebase Firestore中?
P粉215292716
P粉215292716 2024-04-02 15:32:42
0
1
371

我使用 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() 方法的文檔,但我不確定如何繼續。

我需要一些基本程式碼的幫助,以便在他們註冊後提取這些資料。

感謝任何幫助,因為過去兩天我一直在為此抓狂。

P粉215292716
P粉215292716

全部回覆(1)
P粉828463673

無法從 onAuthStateChanged 中的 OAuth 提供者取得其他資訊。

您必須在登入提供者後才能取得該資訊。所以在您致電 Firebase 之前。提供者的結果包含您在步驟 2 中要求的範圍值:https://firebase.google.com/docs/auth/web/google-signin#handle_the_sign-in_flow_with_the_firebase_sdk

#
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!