config-plugin-react-native-auth0
Version:
Expo plugin for react-native-auth0 using expo config plugins
64 lines (63 loc) • 2.54 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.withAuth0AppDelegate = exports.withAuth0InfoPlist = exports.withAuth0IOS = void 0;
const config_plugins_1 = require("@expo/config-plugins");
const config_plugins_2 = require("@expo/config-plugins");
const fs_1 = require("fs");
const withAuth0IOS = (config) => {
config = (0, exports.withAuth0InfoPlist)(config);
config = (0, exports.withAuth0AppDelegate)(config);
return config;
};
exports.withAuth0IOS = withAuth0IOS;
const withAuth0InfoPlist = (config) => {
return (0, config_plugins_1.withInfoPlist)(config, async (config) => {
if (!config.modResults.CFBundleIdentifier) {
config.modResults.CFBundleIdentifier = "$(PRODUCT_BUNDLE_IDENTIFIER)";
}
if (!config.modResults.CFBundleURLTypes) {
config.modResults.CFBundleURLTypes = [
{
CFBundleURLName: "auth0",
CFBundleURLSchemes: ["$(PRODUCT_BUNDLE_IDENTIFIER)"],
},
];
}
return config;
});
};
exports.withAuth0InfoPlist = withAuth0InfoPlist;
const withAuth0AppDelegate = (config) => {
return (0, config_plugins_2.withDangerousMod)(config, [
"ios",
async (config) => {
const fileInfo = config_plugins_2.IOSConfig.Paths.getAppDelegate(config.modRequest.projectRoot);
let contents = await fs_1.promises.readFile(fileInfo.path, "utf-8");
contents = modifyObjcAppDelegate({ contents });
await fs_1.promises.writeFile(fileInfo.path, contents);
return config;
},
]);
};
exports.withAuth0AppDelegate = withAuth0AppDelegate;
function modifyObjcAppDelegate({ contents }) {
// Add import
if (!contents.includes("#import <React/RCTLinkingManager.h>")) {
// Replace the first line with the RCTLinkingManager import
contents = contents.replace(/#import "AppDelegate.h"/g, `#import "AppDelegate.h"\n#import <React/RCTLinkingManager.h>`);
}
const initMethodInvocationBlock = `- (BOOL)application:(UIApplication *)app`;
if (!contents.includes(initMethodInvocationBlock)) {
// TODO: Determine if this is safe
contents = contents.replace(/return YES;/g, `return YES;\n
}
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
return [RCTLinkingManager application:app openURL:url options:options];
}
`);
}
return contents;
}
;