Hello,
I’ve been trying to setup Google Authentication with Expo’s AuthSession. I’m currently using the following code:
const [, response, signInWithGoogle] = Google.useIdTokenAuthRequest({
expoClientId:
"<id for Expo Go>",
androidClientId:
"<id for standalone android app>,
webClientId:
"<id for web>",
});
I use the signInWithGoogle() function to open the Sign In popup and then get the information form the response.
On Android (both Expo Go and Standalone) it works flawlessly. On web though, the popup just reopens my app again inside of it and doesn’t do anything at all (after I select my Google account). If I close the new app the original one gets a “dismiss” response type.
As you mentioned that you were able to authenticate google both in Expo Go and Standalone.
Could you please help me out to authenticate google in the standalone app.
I have tried the below approaches to login via Google but facing issues where are:
When I remove the scheme in the app.json file and try to authenticate google, I am getting a response as type: dismiss instead of success and not getting the access token.
When I added the intent filters of Scheme in the android object from the app.json, try to authenticate google, I am being popped out with two apps.
Could you please let me know what configuration details or approach you have followed.
Here are my packages/version details used:
SDK Version: 44,
Platform: Android
Could you please share a sample code of yours?
Like I am not sure how did you succeed in the google authentication in the Standalone app as the first link provided is still open and if you have succeeded then you have been prompted with a window where the app will be displayed twice to open after you google oauth which describes the second link above.
Please let me know your response and how have you handled the google OAuth for android & iOS in the standalone.
The code I already posted is literally all the code I use of this library. With Google.useIdTokenAuthRequest() I get an id_token with which I then make a credential using firebase’s GoogleAuthProvider.credential() and log in with that. Here’s my Google Sign In button component, with the client IDs removed:
import * as Google from "expo-auth-session/providers/google";
import { Button } from "react-native-paper";
import {
getAuth,
GoogleAuthProvider,
signInWithCredential,
} from "firebase/auth";
import { useEffect } from "react";
import PropTypes from "prop-types";
import styles from "../styles.js";
export function GoogleSignIn({ onSignIn }) {
const auth = getAuth();
const [, response, signInWithGoogle] = Google.useIdTokenAuthRequest({
expoClientId:
"(...).apps.googleusercontent.com",
androidClientId:
"(...).apps.googleusercontent.com",
webClientId:
"(...).apps.googleusercontent.com",
});
useEffect(() => {
if (response?.type != "success") return;
onSignIn(async () => {
const { id_token } = response.params;
const credential = GoogleAuthProvider.credential(id_token);
await signInWithCredential(auth, credential);
});
}, [response]);
return (
<Button onPress={() => signInWithGoogle()} style={styles.button}>
Sign in with Google
</Button>
);
}
GoogleSignIn.propTypes = {
onSignIn: PropTypes.func.isRequired,
};
onSignIn is just a function that executes the parameter within a try closure to catch any errors and display them.