-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathwithAuth0.ts
More file actions
235 lines (209 loc) · 6.67 KB
/
withAuth0.ts
File metadata and controls
235 lines (209 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import {
AndroidConfig,
createRunOncePlugin,
withAppDelegate,
withInfoPlist,
withAndroidManifest,
} from 'expo/config-plugins';
import type {
ConfigPlugin,
ExportedConfigWithProps,
InfoPlist,
} from 'expo/config-plugins';
import { mergeContents } from './generateCode';
let APPLICATION_ID_SUFFIX = '.auth0';
let pkg: { name: string; version?: string } = {
name: 'react-native-auth0',
};
try {
pkg = require('react-native-auth0/package.json');
} catch {
// empty catch block
}
export const addAndroidAuth0Manifest = (
auth0Configs: Auth0PluginConfig[],
manifest: ExportedConfigWithProps<AndroidConfig.Manifest.AndroidManifest>,
applicationId?: string
) => {
if (auth0Configs.length === 0) {
throw new Error(`No auth0 domain specified in expo config`);
}
const intentFilterContent = [
{
action: [{ $: { 'android:name': 'android.intent.action.VIEW' } }],
category: [
{ $: { 'android:name': 'android.intent.category.DEFAULT' } },
{ $: { 'android:name': 'android.intent.category.BROWSABLE' } },
],
data: [],
},
];
const mainApplication = AndroidConfig.Manifest.getMainApplicationOrThrow(
manifest.modResults
);
AndroidConfig.Manifest.ensureToolsAvailable(manifest.modResults);
// Ensure RedirectActivity exists
let redirectActivity = mainApplication.activity?.find(
(activity) =>
activity.$['android:name'] ===
'com.auth0.android.provider.RedirectActivity'
);
if (!redirectActivity) {
redirectActivity = {
'$': {
'android:name': 'com.auth0.android.provider.RedirectActivity',
'tools:node': 'replace',
'android:exported': 'true',
},
'intent-filter': intentFilterContent,
};
mainApplication.activity = mainApplication.activity || [];
mainApplication.activity.push(redirectActivity);
}
redirectActivity['intent-filter'] =
redirectActivity['intent-filter'] || intentFilterContent;
const intentFilter = redirectActivity['intent-filter'][0] || {};
intentFilter.data = intentFilter.data || [];
// Add data elements for each auth0Config
auth0Configs.forEach((config) => {
if (config.domain == null) {
throw new Error(`No auth0 domain specified in expo config`);
}
if (config.customScheme == null && applicationId == null) {
throw new Error(
`No auth0 scheme specified or package found in expo config`
);
}
let auth0Scheme =
config.customScheme ?? applicationId + APPLICATION_ID_SUFFIX;
const dataElement = {
$: {
'android:scheme': auth0Scheme,
'android:pathPrefix': `/android/${applicationId}/callback`,
'android:host': config.domain,
},
};
intentFilter.data?.push(dataElement);
});
return manifest;
};
const withAndroidAuth0Manifest: ConfigPlugin<Auth0PluginConfig[]> = (
config,
auth0Configs
) => {
let applicationId = config.android?.package;
return withAndroidManifest(config, (manifest) => {
return addAndroidAuth0Manifest(auth0Configs, manifest, applicationId);
});
};
export const addAuth0AppDelegateCode = (
src: string,
language: string
): string => {
let tempSrc = src;
// Check if this is a Swift app delegate file by looking for Swift syntax patterns
// rather than relying on import statements that may change
const isSwift = language === 'swift';
if (!isSwift) {
// Throw error for non-Swift files, since we're only supporting Swift app delegates (RN 78+)
throw new Error(
'This plugin only supports expo 53 or greater. If you are using older version 4.x'
);
}
// Swift handling for Expo 53+
// Add URL handling method if it doesn't exist
if (!src.includes('RCTLinkingManager.application')) {
tempSrc = mergeContents({
src: tempSrc,
newSrc: [
' // Handle URL schemes for Auth0 authentication',
' override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {',
' return RCTLinkingManager.application(app, open: url, options: options)',
' }',
].join('\n'),
tag: 'react-native-auth0-linking-swift',
anchor: /override func sourceURL\(for bridge: RCTBridge\)/,
comment: '//',
offset: -1,
}).contents;
}
// In expo 53+ by default RCTLinkingManager is available
return tempSrc;
};
const withIOSAuth0AppDelegate: ConfigPlugin<Auth0PluginConfig[]> = (config) => {
return withAppDelegate(config, (config) => {
const src = config.modResults.contents;
const language = config.modResults.language;
config.modResults.contents = addAuth0AppDelegateCode(src, language);
return config;
});
};
const withIOSAuth0InfoPList: ConfigPlugin<Auth0PluginConfig[]> = (
config,
props
) => {
return withInfoPlist(config, (config) => {
return addIOSAuth0ConfigInInfoPList(props, config);
});
};
export const addIOSAuth0ConfigInInfoPList = (
props: Auth0PluginConfig[],
config: ExportedConfigWithProps<InfoPlist>
) => {
let customSchemes = props
.filter((prop) => prop.customScheme != null)
.map((prop) => prop.customScheme as string);
let bundleIdentifier;
if (config.ios?.bundleIdentifier) {
bundleIdentifier = config.ios?.bundleIdentifier + APPLICATION_ID_SUFFIX;
}
if (customSchemes.length === 0) {
if (bundleIdentifier == null) {
throw Error(
'No auth0 scheme specified or bundle identifier found in expo config'
);
} else {
customSchemes = [bundleIdentifier];
}
}
let urlTypes = config.modResults.CFBundleURLTypes || [];
customSchemes.forEach((scheme) => {
if (
urlTypes.some(({ CFBundleURLSchemes }) =>
CFBundleURLSchemes.includes(scheme)
)
) {
return;
}
const existingAuth0URLType = urlTypes.find(
(urlType) => urlType.CFBundleURLName === 'auth0'
);
if (existingAuth0URLType) {
// Add the scheme to the existing CFBundleURLSchemes array
existingAuth0URLType.CFBundleURLSchemes.push(scheme);
} else {
// Add a new object
urlTypes.push({
CFBundleURLName: 'auth0',
CFBundleURLSchemes: [scheme],
});
}
});
config.modResults.CFBundleURLTypes = urlTypes;
return config;
};
type Auth0PluginConfig = {
customScheme?: string;
domain?: string;
};
const withAuth0: ConfigPlugin<[Auth0PluginConfig] | Auth0PluginConfig> = (
config,
props
) => {
const auth0PluginConfigs = Array.isArray(props) ? props : [props];
config = withAndroidAuth0Manifest(config, auth0PluginConfigs);
config = withIOSAuth0AppDelegate(config, auth0PluginConfigs);
config = withIOSAuth0InfoPList(config, auth0PluginConfigs);
return config;
};
export default createRunOncePlugin(withAuth0, pkg.name, pkg.version);