Skip to content

Commit fd18ca6

Browse files
author
Kadi Kraman
committed
Convert example app to use hooks
1 parent 3e0f64c commit fd18ca6

File tree

1 file changed

+64
-79
lines changed

1 file changed

+64
-79
lines changed

Example/App.js

Lines changed: 64 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React, { useState, useCallback } from 'react';
22
import { UIManager, LayoutAnimation, Alert } from 'react-native';
33
import { authorize, refresh, revoke } from 'react-native-app-auth';
44
import { Page, Button, ButtonContainer, Form, FormLabel, FormValue, Heading } from './components';
@@ -27,106 +27,91 @@ const config = {
2727
// }
2828
};
2929

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-
}
30+
const defaultAuthState = {
31+
hasLoggedInOnce: false,
32+
accessToken: '',
33+
accessTokenExpirationDate: '',
34+
refreshToken: ''
35+
};
36+
37+
export default () => {
38+
const [authState, setAuthState] = useState(defaultAuthState);
4639

47-
authorize = async () => {
40+
const handleAuthorize = useCallback(async () => {
4841
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-
scopes: authState.scopes
58-
},
59-
500
60-
);
42+
const newAuthState = await authorize(config);
43+
44+
setAuthState({
45+
hasLoggedInOnce: true,
46+
...newAuthState
47+
});
48+
6149
} catch (error) {
6250
Alert.alert('Failed to log in', error.message);
6351
}
64-
};
52+
}, [authState]);
6553

66-
refresh = async () => {
54+
const handleRefresh = useCallback(async () => {
6755
try {
68-
const authState = await refresh(config, {
69-
refreshToken: this.state.refreshToken
56+
const newAuthState = await refresh(config, {
57+
refreshToken: authState.refreshToken
7058
});
7159

72-
this.animateState({
73-
accessToken: authState.accessToken || this.state.accessToken,
74-
accessTokenExpirationDate:
75-
authState.accessTokenExpirationDate || this.state.accessTokenExpirationDate,
76-
refreshToken: authState.refreshToken || this.state.refreshToken
77-
});
60+
setAuthState(current => ({
61+
...current,
62+
...newAuthState
63+
}))
64+
7865
} catch (error) {
7966
Alert.alert('Failed to refresh token', error.message);
8067
}
81-
};
68+
}, [authState]);
8269

83-
revoke = async () => {
70+
const handleRevoke = useCallback(async () => {
8471
try {
8572
await revoke(config, {
86-
tokenToRevoke: this.state.accessToken,
73+
tokenToRevoke: authState.accessToken,
8774
sendClientId: true
8875
});
89-
this.animateState({
76+
77+
setAuthState({
9078
accessToken: '',
9179
accessTokenExpirationDate: '',
9280
refreshToken: ''
9381
});
9482
} catch (error) {
9583
Alert.alert('Failed to revoke token', error.message);
9684
}
97-
};
98-
99-
render() {
100-
const { state } = this;
101-
return (
102-
<Page>
103-
{!!state.accessToken ? (
104-
<Form>
105-
<FormLabel>accessToken</FormLabel>
106-
<FormValue>{state.accessToken}</FormValue>
107-
<FormLabel>accessTokenExpirationDate</FormLabel>
108-
<FormValue>{state.accessTokenExpirationDate}</FormValue>
109-
<FormLabel>refreshToken</FormLabel>
110-
<FormValue>{state.refreshToken}</FormValue>
111-
<FormLabel>scopes</FormLabel>
112-
<FormValue>{state.scopes.join(', ')}</FormValue>
113-
</Form>
114-
) : (
115-
<Heading>{state.hasLoggedInOnce ? 'Goodbye.' : 'Hello, stranger.'}</Heading>
116-
)}
117-
118-
<ButtonContainer>
119-
{!state.accessToken ? (
120-
<Button onPress={this.authorize} text="Authorize" color="#DA2536" />
121-
) : null}
122-
{!!state.refreshToken ? (
123-
<Button onPress={this.refresh} text="Refresh" color="#24C2CB" />
124-
) : null}
125-
{!!state.accessToken ? (
126-
<Button onPress={this.revoke} text="Revoke" color="#EF525B" />
127-
) : null}
128-
</ButtonContainer>
129-
</Page>
130-
);
131-
}
85+
}, [authState]);
86+
87+
return (
88+
<Page>
89+
{!!authState.accessToken ? (
90+
<Form>
91+
<FormLabel>accessToken</FormLabel>
92+
<FormValue>{authState.accessToken}</FormValue>
93+
<FormLabel>accessTokenExpirationDate</FormLabel>
94+
<FormValue>{authState.accessTokenExpirationDate}</FormValue>
95+
<FormLabel>refreshToken</FormLabel>
96+
<FormValue>{authState.refreshToken}</FormValue>
97+
<FormLabel>scopes</FormLabel>
98+
<FormValue>{authState.scopes.join(', ')}</FormValue>
99+
</Form>
100+
) : (
101+
<Heading>{authState.hasLoggedInOnce ? 'Goodbye.' : 'Hello, stranger.'}</Heading>
102+
)}
103+
104+
<ButtonContainer>
105+
{!authState.accessToken ? (
106+
<Button onPress={handleAuthorize} text="Authorize" color="#DA2536" />
107+
) : null}
108+
{!!authState.refreshToken ? (
109+
<Button onPress={handleRefresh} text="Refresh" color="#24C2CB" />
110+
) : null}
111+
{!!authState.accessToken ? (
112+
<Button onPress={handleRevoke} text="Revoke" color="#EF525B" />
113+
) : null}
114+
</ButtonContainer>
115+
</Page>
116+
);
132117
}

0 commit comments

Comments
 (0)