简体   繁体   English

React-native expo:谷歌登录适用于 IOS 和 Android,但不适用于 web 浏览器

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

Using expo authSession I was able to make google login work on iOS and android (tested in emulators), i get the accessToken and idToken and all user data properly from google.使用 expo authSession我能够使谷歌登录在 iOS 和 android 上工作(在模拟器中测试),我从谷歌正确地获得了 accessToken 和 idToken 以及所有用户数据。

However when I try the same for web client, it doesnt work.但是,当我对 web 客户端进行相同的尝试时,它不起作用。 Everything is set in google cloud, no error comes up, I can login to google with a success response, but all data is null:一切都在谷歌云中设置,没有错误出现,我可以成功响应登录到谷歌,但所有数据都是 null:

response.type is "success" (see the screenshot), but the authentication object is still null (accessToken is stored there for iOS and Android). response.type 是“成功”(见屏幕截图),但身份验证 object 仍然是 null(accessToken 存储在那里用于 iOS 和 Android)。 Here is the data response I get using web browsers, it shows no error at all: enter image description here这是我使用 web 浏览器得到的数据响应,完全没有显示错误: enter image description here

My code snippet, tried with 'cliendId', 'webClientId', 'expoClientId': `我的代码片段,尝试使用“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)
  }

` `

As I understood, I dont have to do anything differently for web client?据我了解,我不必为 web 客户端做任何不同的事情吗? Maybe I missed something?也许我错过了什么?

Maybe its important: testing in chrome localhost, macOS.也许它很重要:在 chrome 本地主机、macOS 中进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM