簡體   English   中英

React-native expo:谷歌登錄適用於 IOS 和 Android,但不適用於 web 瀏覽器

[英]React-native expo: google login works on IOS and Android, but doesnt work in web browsers

使用 expo authSession我能夠使谷歌登錄在 iOS 和 android 上工作(在模擬器中測試),我從谷歌正確地獲得了 accessToken 和 idToken 以及所有用戶數據。

但是,當我對 web 客戶端進行相同的嘗試時,它不起作用。 一切都在谷歌雲中設置,沒有錯誤出現,我可以成功響應登錄到谷歌,但所有數據都是 null:

response.type 是“成功”(見屏幕截圖),但身份驗證 object 仍然是 null(accessToken 存儲在那里用於 iOS 和 Android)。 這是我使用 web 瀏覽器得到的數據響應,完全沒有顯示錯誤: enter image description here

我的代碼片段,嘗試使用“cliendId”、“webClientId”、“expoClientId”:`

  const [request, response, promptAsync] = Google.useIdTokenAuthRequest({
    //clientId: "594408663...",
    webClientId: "5944086633...",
    //expoClientId: "5944086633...",
    iosClientId: "5944086633...",
    androidClientId: "594408663..."
  })

  React.useEffect(() => {
    if(response?.type == "success") {
      console.log('respons', response)
      setAccessToken(response.authentication?.accessToken)
      setIdToken(response.authentication?.idToken)

      ----------- FAILS HERE AS AUTHENTICATION IS NULL ------------

      accessToken && fetchUserInfo()
    }
  }, [response, accessToken])

  const fetchUserInfo = async () => {
    let userInfoResponse = await fetch("https://www.googleapis.com/userinfo/v2/me", {
      headers: {
        Authorization: `Bearer ${accessToken}`
      }
    })
    
    // creat firebase user entry
    const credential = GoogleAuthProvider.credential(idToken, accessToken)
    await signInWithCredential(auth, credential)

    // create userdata state
    const userData = await userInfoResponse.json()
    setUser(userData)
  }

`

據我了解,我不必為 web 客戶端做任何不同的事情嗎? 也許我錯過了什么?

也許它很重要:在 chrome 本地主機、macOS 中進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM