Skip to content

Commit e7bdbed

Browse files
committed
✨ Add Android support
1 parent 33eedd5 commit e7bdbed

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,13 @@ This is the result from the auth server
265265
```sh
266266
yarn add react-native-app-auth
267267
```
268+
268269
Or
270+
269271
```sh
270272
npm install react-native-app-auth --save
271273
```
272274

273-
274275
## Setup
275276

276277
### iOS Setup
@@ -354,6 +355,7 @@ Furthermore, `RNAppAuth` expects the delegate instance to conform to the protoco
354355
Make `AppDelegate` conform to `RNAppAuthAuthorizationFlowManager` with the following changes to `AppDelegate.h`:
355356
356357
##### For react-native >= 0.68
358+
357359
Example setup can be see in the [Example app](./Example/ios)
358360
359361
```diff

plugin/android/app-build-gradle.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const { AndroidConfig, withDangerousMod } = require('@expo/config-plugins');
2+
const {
3+
createGeneratedHeaderComment,
4+
removeContents,
5+
} = require('@expo/config-plugins/build/utils/generateCode');
6+
const codeModAndroid = require('@expo/config-plugins/build/android/codeMod');
7+
const fs = require('fs');
8+
9+
const withAppAuthAppBuildGradle = (rootConfig, options) =>
10+
withDangerousMod(rootConfig, [
11+
'android',
12+
config => {
13+
// detauls to app scheme
14+
const authScheme = options?.authScheme ?? config.scheme ?? '';
15+
16+
// find the app/build.gradle file and checks its format
17+
const appBuildGradlePath = AndroidConfig.Paths.getAppBuildGradleFilePath(
18+
config.modRequest.projectRoot
19+
);
20+
21+
// BEWARE: we update the app/build.gradle file *outside* of the standard Expo config procedure !
22+
let contents = fs.readFileSync(appBuildGradlePath, 'utf-8');
23+
24+
if (contents.includes('manifestPlaceholders')) {
25+
throw new Error(
26+
'app/build.gradle already contains manifestPlaceholders, cannot update automatically !'
27+
);
28+
}
29+
30+
// let's add the manifestPlaceholders section !
31+
contents = removeContents({
32+
src: contents,
33+
tag: 'react-native-app-auth',
34+
}).contents;
35+
contents = codeModAndroid.appendContentsInsideDeclarationBlock(
36+
contents,
37+
'defaultConfig',
38+
`
39+
${createGeneratedHeaderComment(contents, 'react-native-app-auth', '//')}
40+
manifestPlaceholders = [
41+
'appAuthRedirectScheme': '${authScheme}',
42+
]
43+
// @generated end react-native-app-auth
44+
`
45+
);
46+
47+
// and finally we write the file back to the disk
48+
fs.writeFileSync(appBuildGradlePath, contents, 'utf-8');
49+
50+
return config;
51+
},
52+
]);
53+
54+
module.exports = { withAppAuthAppBuildGradle };

plugin/android/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { withAppAuthAppBuildGradle } = require('./app-build-gradle');
2+
3+
module.exports = {
4+
withAppAuthAppBuildGradle,
5+
};

plugin/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const { withPlugins, createRunOncePlugin } = require('@expo/config-plugins');
22
const { withAppAuthAppDelegate, withAppAuthAppDelegateHeader } = require('./ios');
3+
const { withAppAuthAppBuildGradle } = require('./android');
34

45
const withAppAuth = config => {
56
return withPlugins(config, [
67
// iOS
78
withAppAuthAppDelegate,
89
withAppAuthAppDelegateHeader, // 👈 ️this one uses withDangerousMod !
10+
11+
// Android
12+
withAppAuthAppBuildGradle, // 👈 ️this one uses withDangerousMod !
913
]);
1014
};
1115

0 commit comments

Comments
 (0)