|
| 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 | +} |
0 commit comments