Skip to content

Commit 4218b70

Browse files
author
Kadi Kraman
committed
Fix latest example
1 parent 6ebefa4 commit 4218b70

File tree

83 files changed

+1553
-1624
lines changed

Some content is hidden

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

83 files changed

+1553
-1624
lines changed

Example/Latest/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/Latest/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# React Native App Auth Latest Example
2+
3+
Demo application that uses the version of react-native-app-auth in this repo
4+
5+
## Versions
6+
**React Native** 0.57.2
7+
**App Auth:** Latest
8+
**React Native App Auth:** Latest
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/iOSPodsExample
23+
yarn
24+
```
25+
26+
## Run
27+
28+
```
29+
react-native run-ios
30+
```
31+
32+
```
33+
react-native run-android
34+
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
rootProject.name = 'AppAuthExample'
22
include ':react-native-app-auth'
3-
project(':react-native-app-auth').projectDir = new File(rootProject.projectDir, '../../android')
3+
project(':react-native-app-auth').projectDir = new File(rootProject.projectDir, '../../../android')
44

55
include ':app'

Example/Latest/assets/background.jpg

78.4 KB
Loading

Example/Latest/components/Button.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @flow
2+
3+
import React, { Component } from 'react';
4+
import { Platform } from 'react-native';
5+
import styled from 'styled-components/native';
6+
7+
type Props = {
8+
text: string,
9+
color: string,
10+
onPress: () => any
11+
};
12+
13+
const ButtonBox = styled.TouchableOpacity.attrs({ activeOpacity: 0.8 })`
14+
height: 50px;
15+
flex: 1;
16+
margin: 5px;
17+
align-items: center;
18+
justify-content: center;
19+
background-color: ${props => props.color};
20+
`;
21+
22+
const ButtonText = styled.Text`
23+
color: white;
24+
`;
25+
26+
const Button = ({ text, color, onPress }: Props) => (
27+
<ButtonBox onPress={onPress} color={color}>
28+
<ButtonText>{text}</ButtonText>
29+
</ButtonBox>
30+
);
31+
32+
export default Button;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @flow
2+
3+
import styled from 'styled-components/native';
4+
5+
export default styled.View`
6+
position: absolute;
7+
left: 0;
8+
right: 0;
9+
bottom: 0;
10+
align-self: flex-end;
11+
flex-direction: row;
12+
margin: 5px;
13+
`;

Example/Latest/components/Form.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @flow
2+
3+
import styled from 'styled-components/native';
4+
5+
const Form = styled.View`
6+
flex: 1;
7+
`;
8+
9+
Form.Label = styled.Text`
10+
font-size: 14px;
11+
font-weight: bold;
12+
background-color: transparent;
13+
margin-bottom: 10px;
14+
`;
15+
16+
Form.Value = styled.Text.attrs({ numberOfLines: 10, ellipsizeMode: 'tail' })`
17+
font-size: 14px;
18+
background-color: transparent;
19+
margin-bottom: 20px;
20+
`;
21+
22+
export default Form;

Example/Latest/components/Heading.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @flow
2+
3+
import { Platform } from 'react-native';
4+
import styled from 'styled-components/native';
5+
6+
const font = Platform.select({
7+
ios: 'GillSans-light',
8+
android: 'sans-serif-thin'
9+
});
10+
11+
export default styled.Text`
12+
color: black;
13+
font-family: ${font};
14+
font-size: 32px;
15+
margin-top: 120px;
16+
background-color: transparent;
17+
text-align: center;
18+
`;

Example/Latest/components/Page.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @flow
2+
3+
import styled from 'styled-components/native';
4+
5+
export default styled.ImageBackground.attrs({
6+
source: require('../assets/background.jpg')
7+
})`
8+
flex: 1;
9+
background-color: white;
10+
padding: 40px 10px 10px 10px;
11+
`;

Example/Latest/components/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @flow
2+
3+
export { default as Button } from './Button';
4+
export { default as ButtonContainer } from './ButtonContainer';
5+
export { default as Form } from './Form';
6+
export { default as Heading } from './Heading';
7+
export { default as Page } from './Page';

0 commit comments

Comments
 (0)