#Expo app scheme as hostname to solve OAuth?

2 messages · Page 1 of 1 (latest)

sharp umbra
#

Current implementation:

import { Account, Avatars, Client, OAuthProvider } from 'react-native-appwrite';
import * as WebBrowser from 'expo-web-browser';
import { makeRedirectUri } from 'expo-auth-session';
import { env } from '@/config/env';

export const client = new Client();

client
  .setEndpoint(env.APPWRITE_ENDPOINT)
  .setProject(env.APPWRITE_PROJECT_ID)
  .setPlatform(env.APPWWRITE_PLATFORM);

export const avatar = new Avatars(client);
export const account = new Account(client);

export async function googleLogin() {
  const redirectTo = makeRedirectUri();
  const deepLinkRoute = 'some-route' // Needs to match platform custom host AND be a valid route

  const redirectUri = redirectTo.includes('exp://')
    ? redirectTo
    : `${redirectTo}${deepLinkRoute}`;
  // Development Build: my-scheme://{deepLinkRoute}
  // Expo Go: exp://127.0.0.1:8081/--/{deepLinkRoute}

  const response = account.createOAuth2Token(OAuthProvider.Google, redirectUri);

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

  const browserResponse = await WebBrowser.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 maybeActiveSession = await account.get();
  
  if (!maybeActiveSession) {
    await account.createSession(userId, secret);
  }
}
wild spoke
#

Please delete this and continue on in your previous thread since it's still the same oauth problem