Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth, iOS): Expo plugin: skip expo-router for auth URLs #8203

Merged
merged 19 commits into from
Jan 2, 2025
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading