0
I’m developing an app with PHP in the backend and React Turn on the mobile frontend. The app already has a web frontend written with jquery, and the authentication is done using Google Oauth. In the web version is working like this:
The user clicks on a button and is authenticated by the onSignIn function:
function onSignIn(googleUser) {
var googleToken = googleUser.getAuthResponse().id_token;
$.post('index.php', {
url: 'login',
googleToken: googleToken
});
}
The variable googleToken
stores the value returned by id_token
and is sent to the server, which requests the Google api:
file_get_contents('https://www.googleapis.com/oauth2/v3/tokeninfo?id_token='.$googleToken);
Being a valid token, it returns various information such as name, email and avatar, which are used in authentication, if the email already exists, is logged in, otherwise registered.
In the mobile app, I am developing with Expo and would like to authenticate in the same way, however I am not getting access to id_token
as on the web, but only to a token called access_token
, that does not have the necessary information (such as name, email and avatar) to properly authenticate. Here is an example:
import React, { useEffect } from 'react';
import { Platform } from 'react-native';
import * as WebBrowser from 'expo-web-browser';
import { makeRedirectUri, ResponseType, useAuthRequest, useAutoDiscovery } from 'expo-auth-session';
WebBrowser.maybeCompleteAuthSession();
const useProxy = Platform.select({ web:false, default:true });
export default function Home() {
const discovery = useAutoDiscovery('https://accounts.google.com');
const [request, response, promptAsync] = useAuthRequest(
{
responseType: ResponseType.Token,
usePKCE: false,
clientId: 'CLIENT_ID',
redirectUri: makeRedirectUri({
native: 'com.googleusercontent.apps.GOOGLE_GUID://redirect',
useProxy
}),
scopes: ['openid', 'profile', 'email']
},
discovery
);
useEffect(() => {
if (response?.type === 'success') {
const { access_token } = response.params;
}
}, [response]);
return(
...
);
}
- How to authenticate using Google in an Expo app?
- How to access the
id_token
in the Expo as in the web? - Or, how to use the
access_token
to access profile information, such as name, email and avatar at the Expo?
Before starting the project I gave a studied in all the documentations and I did not find any limitation that could hinder the development, so I started with the Expo because it facilitates in many things, mainly because I still don’t know how to use React Native on the web without it. My problem is simply to find a way to recover the name, email and avatar of the Oauth api to authenticate, on the web it is very easy so I did not imagine that I would have so much headache, I am already many days searching documentation for a solution.
– Matheus