Skip to content

Commit

Permalink
fix(auth, iOS): Expo plugin: don't invoke expo-router for openURL fro…
Browse files Browse the repository at this point in the history
…m recaptcha
  • Loading branch information
jey committed Dec 26, 2024
1 parent 3623a50 commit 97dc2a9
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion packages/auth/plugin/src/ios/urlTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ import {
ConfigPlugin,
IOSConfig,
withInfoPlist,
withAppDelegate,
ExportedConfigWithProps,
} from '@expo/config-plugins';
import type { AppDelegateProjectFile } from '@expo/config-plugins/build/ios/Paths';
import { mergeContents } from '@expo/config-plugins/build/utils/generateCode';
import fs from 'fs';
import path from 'path';
import plist from 'plist';

// does this for you: https://firebase.google.com/docs/auth/ios/phone-auth#enable-phone-number-sign-in-for-your-firebase-project
export const withIosCaptchaUrlTypes: ConfigPlugin = config => {
return withInfoPlist(config, config => {
config = withInfoPlist(config, config => {
return setUrlTypesForCaptcha({ config });
});
config = withAppDelegate(config, config => {
return patchOpenUrlForCaptcha({ config });
});
return config;
};

function getReversedClientId(googleServiceFilePath: string): string {
Expand Down Expand Up @@ -88,3 +95,48 @@ export function setUrlTypesForCaptcha({

return config;
}

const skipOpenUrlForFirebaseAuthBlock = `\
if ([url.host caseInsensitiveCompare:@"firebaseauth"] == NSOrderedSame) {
// invocations for Firebase Auth are handled elsewhere and should not be forwarded to Expo Router
return NO;
}\
`;

// NOTE: `mergeContents()` requires that this pattern not match newlines
const appDelegateOpenUrlInsertionPointAfter =
/-\s*\(\s*BOOL\s*\)\s*application\s*:\s*\(\s*UIApplication\s*\*\s*\)\s*application\s+openURL\s*:\s*\(\s*NSURL\s*\*\s*\)\s*url\s+options\s*:\s*\(\s*NSDictionary\s*<\s*UIApplicationOpenURLOptionsKey\s*,\s*id\s*>\s*\*\s*\)\s*options\s*/; // 🙈

function patchOpenUrlForCaptcha({ config }: {
config: ExportedConfigWithProps<AppDelegateProjectFile>;
}) {
const {contents} = config.modResults;
const multilineMatcher = new RegExp(appDelegateOpenUrlInsertionPointAfter.source + '\\s*{\\s*\\n');
const fullMatch = contents.match(multilineMatcher);
if(!fullMatch) {
throw new Error("Failed to find insertion point; expected newline after '{'");
}
const fullMatchNumLines = fullMatch[0].split('\n').length;
const offset = fullMatchNumLines - 1;
if(offset < 0) {
throw new Error(`Failed to find insertion point; fullMatchNumLines=${fullMatchNumLines}`);
}

const newContents = mergeContents({
tag: '@react-native-firebase/auth-openURL',
src: contents,
newSrc: skipOpenUrlForFirebaseAuthBlock ,
anchor: appDelegateOpenUrlInsertionPointAfter,
offset,
comment: '//',
}).contents;

const newConfig = {
...config,
modResults: {
...config.modResults,
contents: newContents,
},
};
return newConfig;
}

0 comments on commit 97dc2a9

Please sign in to comment.