8

At moment im using this snippet of code to sign in to google, but i cant get user email… anyone know how to do this?

var LoginGoogle = () => {

  const [request, response, promptAsync] = Google.useAuthRequest({

    androidClientId: 'xxxxxxx.apps.googleusercontent.com',

    expoClientId: 'xxxxxxx.apps.googleusercontent.com'

  },{

    scopes: ["email"]

  },{});

  

  React.useEffect(() => {

    if (response?.type === 'success') {

      const { authentication } = response;

      console.log(response);

      }

  }, [response]);

  return (

    <GoogleSocialButton disabled={!request} onPress={() => {promptAsync()}} />

  ) 

}

response returns object with links instead of email

Jacs
  • 1,437
  • 4
  • 21
  • 31

2 Answers2

8

I wish this is written in the expo docs. I would like to add a few points from the first answer:

  1. First if you need code snippets on how to fetch user data after getting the access token, you can refer to this github issue: https://github.com/expo/expo/issues/8384

access token can be received by the following code after receiving the response object:

const { authentication: { accessToken } } = response;

then, you can create a function like this:

async function fetchUserInfo(token) {
  const response = await fetch('https://www.googleapis.com/oauth2/v3/userinfo', {
    method: 'GET',
    headers: {
      Accept: 'application/json',
      Authorization: `Bearer ${token}`,
      'Content-Type': 'application/json'
    },
  });

  return await response.json();
}

and get the user object (which contains the user email, profile, photo, etc) by something like this inside an async function:

const user = await fetchUserInfo(accessToken);
  1. But NOTE for the user object, using https://www.googleapis.com/oauth2/v2/userinfo and https://www.googleapis.com/oauth2/v3/userinfo, will yield slightly different result/object ; in particular for v3, since Google implements the OpenID Connect API, there is no "id" attribute anymore, "id" will be called "sub".

sources:

Example of a user object in v3:

Object {
  "email": "xxxxx@gmail.com",
  "email_verified": true,
  "family_name": "John Deer",
  "given_name": "John",
  "hd": "gmail.com",
  "locale": "en",
  "name": "John Deer",
  "picture": "https://lh3.googleusercontent.com/a/asdfjasdklfjaslkf",
  "sub": "10998837733652322",
}

Hope this helps someone in the future...!

EDIT: if you need the id_token checkout this one: expo-auth-session/providers/google Google.useAuthRequest

Ammar Faris
  • 101
  • 1
  • 6
6

I am using AuthSession as well in my RN app and I stumbled with this problem. After going through Google API Docs, found out you can pass the access token from the useAuthRequest response to https://www.googleapis.com/oauth2/v3/userinfo?access_token= ACCESS_TOKEN.

  • 1
    Well, I am really grateful for the Expo team for the APIs they provide to us for easy authentication in React Native. However, it would be great if they could give a slight hint to inform us that we need to visit the individual docs page of the provider to proceed. Or did I miss it out ;/, thanks for your answer anyway! – Jarrett Oct 12 '21 at 09:01
  • May I ask which documentation did you get this link from? – Jarrett Oct 12 '21 at 09:26