|
1 |
| -import React, { Component } from 'react'; |
| 1 | +import React, { useState, useCallback } from 'react'; |
2 | 2 | import { UIManager, LayoutAnimation, Alert } from 'react-native';
|
3 | 3 | import { authorize, refresh, revoke } from 'react-native-app-auth';
|
4 | 4 | import { Page, Button, ButtonContainer, Form, FormLabel, FormValue, Heading } from './components';
|
@@ -27,106 +27,91 @@ const config = {
|
27 | 27 | // }
|
28 | 28 | };
|
29 | 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 |
| - } |
| 30 | +const defaultAuthState = { |
| 31 | + hasLoggedInOnce: false, |
| 32 | + accessToken: '', |
| 33 | + accessTokenExpirationDate: '', |
| 34 | + refreshToken: '' |
| 35 | +}; |
| 36 | + |
| 37 | +export default () => { |
| 38 | + const [authState, setAuthState] = useState(defaultAuthState); |
46 | 39 |
|
47 |
| - authorize = async () => { |
| 40 | + const handleAuthorize = useCallback(async () => { |
48 | 41 | 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 | + |
61 | 49 | } catch (error) {
|
62 | 50 | Alert.alert('Failed to log in', error.message);
|
63 | 51 | }
|
64 |
| - }; |
| 52 | + }, [authState]); |
65 | 53 |
|
66 |
| - refresh = async () => { |
| 54 | + const handleRefresh = useCallback(async () => { |
67 | 55 | try {
|
68 |
| - const authState = await refresh(config, { |
69 |
| - refreshToken: this.state.refreshToken |
| 56 | + const newAuthState = await refresh(config, { |
| 57 | + refreshToken: authState.refreshToken |
70 | 58 | });
|
71 | 59 |
|
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 | + |
78 | 65 | } catch (error) {
|
79 | 66 | Alert.alert('Failed to refresh token', error.message);
|
80 | 67 | }
|
81 |
| - }; |
| 68 | + }, [authState]); |
82 | 69 |
|
83 |
| - revoke = async () => { |
| 70 | + const handleRevoke = useCallback(async () => { |
84 | 71 | try {
|
85 | 72 | await revoke(config, {
|
86 |
| - tokenToRevoke: this.state.accessToken, |
| 73 | + tokenToRevoke: authState.accessToken, |
87 | 74 | sendClientId: true
|
88 | 75 | });
|
89 |
| - this.animateState({ |
| 76 | + |
| 77 | + setAuthState({ |
90 | 78 | accessToken: '',
|
91 | 79 | accessTokenExpirationDate: '',
|
92 | 80 | refreshToken: ''
|
93 | 81 | });
|
94 | 82 | } catch (error) {
|
95 | 83 | Alert.alert('Failed to revoke token', error.message);
|
96 | 84 | }
|
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 | + ); |
132 | 117 | }
|
0 commit comments