Skip to content

Commit 6ebefa4

Browse files
author
Kadi Kraman
committed
Add an android example
1 parent 00bc940 commit 6ebefa4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+7358
-0
lines changed

Example/AndroidExample/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["module:metro-react-native-babel-preset"]
3+
}

Example/AndroidExample/.buckconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
[android]
3+
target = Google Inc.:Google APIs:23
4+
5+
[maven_repositories]
6+
central = https://repo1.maven.org/maven2

Example/AndroidExample/.flowconfig

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
[ignore]
2+
; We fork some components by platform
3+
.*/*[.]android.js
4+
5+
; Ignore "BUCK" generated dirs
6+
<PROJECT_ROOT>/\.buckd/
7+
8+
; Ignore unexpected extra "@providesModule"
9+
.*/node_modules/.*/node_modules/fbjs/.*
10+
11+
; Ignore duplicate module providers
12+
; For RN Apps installed via npm, "Libraries" folder is inside
13+
; "node_modules/react-native" but in the source repo it is in the root
14+
.*/Libraries/react-native/React.js
15+
16+
; Ignore polyfills
17+
.*/Libraries/polyfills/.*
18+
19+
; Ignore metro
20+
.*/node_modules/metro/.*
21+
22+
[include]
23+
24+
[libs]
25+
node_modules/react-native/Libraries/react-native/react-native-interface.js
26+
node_modules/react-native/flow/
27+
node_modules/react-native/flow-github/
28+
29+
[options]
30+
emoji=true
31+
32+
esproposal.optional_chaining=enable
33+
esproposal.nullish_coalescing=enable
34+
35+
module.system=haste
36+
module.system.haste.use_name_reducers=true
37+
# get basename
38+
module.system.haste.name_reducers='^.*/\([a-zA-Z0-9$_.-]+\.js\(\.flow\)?\)$' -> '\1'
39+
# strip .js or .js.flow suffix
40+
module.system.haste.name_reducers='^\(.*\)\.js\(\.flow\)?$' -> '\1'
41+
# strip .ios suffix
42+
module.system.haste.name_reducers='^\(.*\)\.ios$' -> '\1'
43+
module.system.haste.name_reducers='^\(.*\)\.android$' -> '\1'
44+
module.system.haste.name_reducers='^\(.*\)\.native$' -> '\1'
45+
module.system.haste.paths.blacklist=.*/__tests__/.*
46+
module.system.haste.paths.blacklist=.*/__mocks__/.*
47+
module.system.haste.paths.blacklist=<PROJECT_ROOT>/node_modules/react-native/Libraries/Animated/src/polyfills/.*
48+
module.system.haste.paths.whitelist=<PROJECT_ROOT>/node_modules/react-native/Libraries/.*
49+
50+
munge_underscores=true
51+
52+
module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub'
53+
54+
module.file_ext=.js
55+
module.file_ext=.jsx
56+
module.file_ext=.json
57+
module.file_ext=.native.js
58+
59+
suppress_type=$FlowIssue
60+
suppress_type=$FlowFixMe
61+
suppress_type=$FlowFixMeProps
62+
suppress_type=$FlowFixMeState
63+
64+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)
65+
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+
66+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
67+
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError
68+
69+
[version]
70+
^0.78.0

Example/AndroidExample/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

Example/AndroidExample/.gitignore

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# OSX
2+
#
3+
.DS_Store
4+
5+
# Xcode
6+
#
7+
build/
8+
*.pbxuser
9+
!default.pbxuser
10+
*.mode1v3
11+
!default.mode1v3
12+
*.mode2v3
13+
!default.mode2v3
14+
*.perspectivev3
15+
!default.perspectivev3
16+
xcuserdata
17+
*.xccheckout
18+
*.moved-aside
19+
DerivedData
20+
*.hmap
21+
*.ipa
22+
*.xcuserstate
23+
project.xcworkspace
24+
25+
# Android/IntelliJ
26+
#
27+
build/
28+
.idea
29+
.gradle
30+
local.properties
31+
*.iml
32+
33+
# node.js
34+
#
35+
node_modules/
36+
npm-debug.log
37+
yarn-error.log
38+
39+
# BUCK
40+
buck-out/
41+
\.buckd/
42+
*.keystore
43+
44+
# fastlane
45+
#
46+
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
47+
# screenshots whenever they are needed.
48+
# For more information about the recommended setup visit:
49+
# https://docs.fastlane.tools/best-practices/source-control/
50+
51+
*/fastlane/report.xml
52+
*/fastlane/Preview.html
53+
*/fastlane/screenshots
54+
55+
# Bundle artifact
56+
*.jsbundle
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

Example/AndroidExample/App.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import React, { Component } from 'react';
2+
import { UIManager, LayoutAnimation, Alert } from 'react-native';
3+
import { authorize, refresh, revoke } from 'react-native-app-auth';
4+
import { Page, Button, ButtonContainer, Form, Heading } from './components';
5+
6+
UIManager.setLayoutAnimationEnabledExperimental &&
7+
UIManager.setLayoutAnimationEnabledExperimental(true);
8+
9+
type State = {
10+
hasLoggedInOnce: boolean,
11+
accessToken: ?string,
12+
accessTokenExpirationDate: ?string,
13+
refreshToken: ?string
14+
};
15+
16+
const config = {
17+
issuer: 'https://demo.identityserver.io',
18+
clientId: 'native.code',
19+
redirectUrl: 'io.identityserver.demo:/oauthredirect',
20+
additionalParameters: {},
21+
scopes: ['openid', 'profile', 'email', 'offline_access']
22+
23+
// serviceConfiguration: {
24+
// authorizationEndpoint: 'https://demo.identityserver.io/connect/authorize',
25+
// tokenEndpoint: 'https://demo.identityserver.io/connect/token',
26+
// revocationEndpoint: 'https://demo.identityserver.io/connect/revoke'
27+
// }
28+
};
29+
30+
export default class App extends Component<{}, State> {
31+
state = {
32+
hasLoggedInOnce: false,
33+
accessToken: '',
34+
accessTokenExpirationDate: '',
35+
refreshToken: ''
36+
};
37+
38+
animateState(nextState: $Shape<State>, delay: number = 0) {
39+
setTimeout(() => {
40+
this.setState(() => {
41+
LayoutAnimation.easeInEaseOut();
42+
return nextState;
43+
});
44+
}, delay);
45+
}
46+
47+
authorize = async () => {
48+
try {
49+
const authState = await authorize(config);
50+
51+
this.animateState(
52+
{
53+
hasLoggedInOnce: true,
54+
accessToken: authState.accessToken,
55+
accessTokenExpirationDate: authState.accessTokenExpirationDate,
56+
refreshToken: authState.refreshToken
57+
},
58+
500
59+
);
60+
} catch (error) {
61+
Alert.alert('Failed to log in', error.message);
62+
}
63+
};
64+
65+
refresh = async () => {
66+
try {
67+
const authState = await refresh(config, {
68+
refreshToken: this.state.refreshToken
69+
});
70+
71+
this.animateState({
72+
accessToken: authState.accessToken || this.state.accessToken,
73+
accessTokenExpirationDate:
74+
authState.accessTokenExpirationDate || this.state.accessTokenExpirationDate,
75+
refreshToken: authState.refreshToken || this.state.refreshToken
76+
});
77+
} catch (error) {
78+
Alert.alert('Failed to refresh token', error.message);
79+
}
80+
};
81+
82+
revoke = async () => {
83+
try {
84+
await revoke(config, {
85+
tokenToRevoke: this.state.accessToken,
86+
sendClientId: true
87+
});
88+
this.animateState({
89+
accessToken: '',
90+
accessTokenExpirationDate: '',
91+
refreshToken: ''
92+
});
93+
} catch (error) {
94+
Alert.alert('Failed to revoke token', error.message);
95+
}
96+
};
97+
98+
render() {
99+
const { state } = this;
100+
return (
101+
<Page>
102+
{!!state.accessToken ? (
103+
<Form>
104+
<Form.Label>accessToken</Form.Label>
105+
<Form.Value>{state.accessToken}</Form.Value>
106+
<Form.Label>accessTokenExpirationDate</Form.Label>
107+
<Form.Value>{state.accessTokenExpirationDate}</Form.Value>
108+
<Form.Label>refreshToken</Form.Label>
109+
<Form.Value>{state.refreshToken}</Form.Value>
110+
</Form>
111+
) : (
112+
<Heading>{state.hasLoggedInOnce ? 'Goodbye.' : 'Hello, stranger.'}</Heading>
113+
)}
114+
115+
<ButtonContainer>
116+
{!state.accessToken && (
117+
<Button onPress={this.authorize} text="Authorize" color="#DA2536" />
118+
)}
119+
{!!state.refreshToken && <Button onPress={this.refresh} text="Refresh" color="#24C2CB" />}
120+
{!!state.accessToken && <Button onPress={this.revoke} text="Revoke" color="#EF525B" />}
121+
</ButtonContainer>
122+
</Page>
123+
);
124+
}
125+
}

Example/AndroidExample/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# React Native App Auth Android Example
2+
3+
Demo application for Android
4+
5+
## Versions
6+
**React Native** 0.57.2
7+
**App Auth:** 0.94.0
8+
**React Native App Auth:** 3.1.0
9+
10+
## Setup
11+
12+
You'll need to have Yarn and the React Native CLI installed:
13+
14+
```sh
15+
npm install --global yarn react-native-cli
16+
```
17+
18+
Clone the repo and install dependencies
19+
20+
```sh
21+
git clone https://github.com/FormidableLabs/react-native-app-auth.git
22+
cd ./react-native-app-auth/Example/AndroidExample
23+
yarn
24+
```
25+
26+
## Run
27+
28+
```
29+
react-native run-android
30+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# To learn about Buck see [Docs](https://buckbuild.com/).
2+
# To run your application with Buck:
3+
# - install Buck
4+
# - `npm start` - to start the packager
5+
# - `cd android`
6+
# - `keytool -genkey -v -keystore keystores/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"`
7+
# - `./gradlew :app:copyDownloadableDepsToLibs` - make all Gradle compile dependencies available to Buck
8+
# - `buck install -r android/app` - compile, install and run application
9+
#
10+
11+
lib_deps = []
12+
13+
for jarfile in glob(['libs/*.jar']):
14+
name = 'jars__' + jarfile[jarfile.rindex('/') + 1: jarfile.rindex('.jar')]
15+
lib_deps.append(':' + name)
16+
prebuilt_jar(
17+
name = name,
18+
binary_jar = jarfile,
19+
)
20+
21+
for aarfile in glob(['libs/*.aar']):
22+
name = 'aars__' + aarfile[aarfile.rindex('/') + 1: aarfile.rindex('.aar')]
23+
lib_deps.append(':' + name)
24+
android_prebuilt_aar(
25+
name = name,
26+
aar = aarfile,
27+
)
28+
29+
android_library(
30+
name = "all-libs",
31+
exported_deps = lib_deps,
32+
)
33+
34+
android_library(
35+
name = "app-code",
36+
srcs = glob([
37+
"src/main/java/**/*.java",
38+
]),
39+
deps = [
40+
":all-libs",
41+
":build_config",
42+
":res",
43+
],
44+
)
45+
46+
android_build_config(
47+
name = "build_config",
48+
package = "com.rnappauthandroidexample",
49+
)
50+
51+
android_resource(
52+
name = "res",
53+
package = "com.rnappauthandroidexample",
54+
res = "src/main/res",
55+
)
56+
57+
android_binary(
58+
name = "app",
59+
keystore = "//android/keystores:debug",
60+
manifest = "src/main/AndroidManifest.xml",
61+
package_type = "debug",
62+
deps = [
63+
":app-code",
64+
],
65+
)

0 commit comments

Comments
 (0)