简体   繁体   中英

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.

However when I try the same for web client, it doesnt work. Everything is set in google cloud, no error comes up, I can login to google with a success response, but all data is null:

response.type is "success" (see the screenshot), but the authentication object is still null (accessToken is stored there for iOS and Android). Here is the data response I get using web browsers, it shows no error at all: enter image description here

My code snippet, tried with '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? Maybe I missed something?

Maybe its important: testing in chrome localhost, macOS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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