react-nativeexpogoogle-oauth

Google Auth in expo 49


I'm following a tutorial from 2 months ago for how to use google-auth, on my expo app. there were a few outdated things, related to Expo-CLI changes, but i managed to learn how to use ESA-CLI for that.

Everything works when i run web, the whole authentication flow. When i try Android or iOS, i get the same error in both, after trying to authenticate:

Error 400: url_redirect_mismatch

I landed on this question / answer: expo-auth-google to expo-auth-session and followed the instructions on the answer,

import { makeRedirectUri } from 'expo-auth-session'
...

const [request, response, promptAsync] = Google.useAuthRequest({
    ...googleConfig,
    redirectUri: makeRedirectUri(),
  })

and i got a different error:

Error 400: invalid_request
Request details: redirect_uri=expo-google-auth://

I have no idea how to fix it and any help would be appreciated


Solution

  • this is working for me

    import { makeRedirectUri } from 'expo-auth-session';
    import Constants from 'expo-constants';
    import * as Google from 'expo-auth-session/providers/google';
    
    const EXPO_REDIRECT_PARAMS = {
      useProxy: true,
      projectNameForProxy: '@yourUsername/yourScheme',
    };
    
    const NATIVE_REDIRECT_PARAMS = { native: 'yourScheme://' };
    
    const REDIRECT_PARAMS =
      Constants.appOwnership === 'expo'
        ? EXPO_REDIRECT_PARAMS
        : NATIVE_REDIRECT_PARAMS;
    
    const GOOGLE_CONFIG = {
      androidClientId: '...',
      webClientId: '...',
      iosClientId: '...',
      redirectUri: makeRedirectUri(REDIRECT_PARAMS),
    };
    
    const LoginPage = ()=> {
         const [request, response, promptAsync] = Google.useAuthRequest(GOOGLE_CONFIG)
         ...
    
    }