Skip to content
Back

Google OAuth not working due to invalid redirect URI, Expo Dev Build

  • 2
  • React Native
  • Cloud
  • Auth
darfdx
24 Dec, 2024, 01:31

I am trying to do OAuth after watching a tutorial on YT, the difference being the tutorial uses Expo Go and I am using a dev build. When trying to login, I create a linking url to my index screen with Linking.createURL('/'), which returns app-scheme:///. When trying to open the browser to login with Google, I get an error 400: Invalid 'success' param: URL host must be one of: localhost, cloud.appwrite.io, appwrite.io, *. If I use Expo Go, it works correctly, but it gives me that error when using the dev build. Can somebody help me?

Login function:

TypeScript
export async function googleLogin() {
  const redirectUri = Linking.createURL('/', { isTripleSlashed: true });
  const response = await account.createOAuth2Session(
    OAuthProvider.Google,
    redirectUri
  );

  if (!response) {
    throw new Error('Failed to create OAuth2 session.');
  }

  const browserResponse = await openAuthSessionAsync(
    response.toString(),
    redirectUri
  );

  if (browserResponse.type !== 'success') {
    throw new Error('Failed to authenticate with Google.');
  }

  const url = new URL(browserResponse.url);

  const secret = url.searchParams.get('secret')?.toString();
  const userId = url.searchParams.get('userId')?.toString();

  if (!secret || !userId) {
    throw new Error('Could not find values in URL.');
  }

  const session = await account.createSession(userId, secret);

  if (!session) {
    throw new Error('Failed to create session');
  }
}

Let me know if you need more context / code. Thanks!

TL;DR
Google OAuth not working with Expo dev build due to invalid redirect URI error 400. Using `Linking.createURL('/')` returns `app-scheme:///`. Issue arises when opening browser to login with Google, error states URL host must be: localhost, cloud.appwrite.io, appwrite.io, *. Works fine with Expo Go but not dev build. Solution: Update Linking.createURL to use a valid domain like localhost or appwrite.io.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more