|
1 |
| -import React, { Component } from 'react' |
| 1 | +import React, { Component } from "react"; |
2 | 2 | import {
|
3 | 3 | StyleSheet,
|
4 | 4 | Text,
|
5 | 5 | View,
|
6 | 6 | AppRegistry,
|
7 | 7 | TouchableHighlight
|
8 |
| -} from 'react-native' |
9 |
| - |
10 |
| -import { NativeRouter, Route, Link, Redirect, withRouter } from 'react-router-native' |
11 |
| - |
12 |
| -const AuthExample = () => ( |
13 |
| - <NativeRouter> |
14 |
| - <View style={styles.container}> |
15 |
| - <AuthButton/> |
16 |
| - <View style={styles.nav}> |
17 |
| - <Link |
18 |
| - to="/public" |
19 |
| - style={styles.navItem} |
20 |
| - underlayColor='#f0f4f7'> |
| 8 | +} from "react-native"; |
| 9 | + |
| 10 | +import { |
| 11 | + NativeRouter, |
| 12 | + Route, |
| 13 | + Link, |
| 14 | + Redirect, |
| 15 | + withRouter |
| 16 | +} from "react-router-native"; |
| 17 | + |
| 18 | +function AuthExample() { |
| 19 | + return ( |
| 20 | + <NativeRouter> |
| 21 | + <View style={styles.container}> |
| 22 | + <AuthButton /> |
| 23 | + <View style={styles.nav}> |
| 24 | + <Link to="/public" style={styles.navItem} underlayColor="#f0f4f7"> |
21 | 25 | <Text>Public Page</Text>
|
22 |
| - </Link> |
23 |
| - <Link |
24 |
| - to="/protected" |
25 |
| - style={styles.navItem} |
26 |
| - underlayColor='#f0f4f7'> |
| 26 | + </Link> |
| 27 | + <Link to="/protected" style={styles.navItem} underlayColor="#f0f4f7"> |
27 | 28 | <Text>Protected Page</Text>
|
28 |
| - </Link> |
29 |
| - </View> |
| 29 | + </Link> |
| 30 | + </View> |
30 | 31 |
|
31 |
| - <Route path="/public" component={Public}/> |
32 |
| - <Route path="/login" component={Login}/> |
33 |
| - <PrivateRoute path="/protected" component={Protected}/> |
34 |
| - </View> |
35 |
| - </NativeRouter> |
36 |
| -) |
| 32 | + <Route path="/public" component={Public} /> |
| 33 | + <Route path="/login" component={Login} /> |
| 34 | + <PrivateRoute path="/protected" component={Protected} /> |
| 35 | + </View> |
| 36 | + </NativeRouter> |
| 37 | + ); |
| 38 | +} |
37 | 39 |
|
38 | 40 | const fakeAuth = {
|
39 | 41 | isAuthenticated: false,
|
40 | 42 | authenticate(cb) {
|
41 |
| - this.isAuthenticated = true |
42 |
| - setTimeout(cb, 100) // fake async |
| 43 | + this.isAuthenticated = true; |
| 44 | + setTimeout(cb, 100); // fake async |
43 | 45 | },
|
44 | 46 | signout(cb) {
|
45 |
| - this.isAuthenticated = false |
46 |
| - setTimeout(cb, 100) |
| 47 | + this.isAuthenticated = false; |
| 48 | + setTimeout(cb, 100); |
47 | 49 | }
|
48 |
| -} |
| 50 | +}; |
49 | 51 |
|
50 |
| -const AuthButton = withRouter(({ history }) => ( |
51 |
| - fakeAuth.isAuthenticated ? ( |
52 |
| - <View> |
53 |
| - <Text>Welcome!</Text> |
54 |
| - <TouchableHighlight style={styles.btn} underlayColor='#f0f4f7' onPress={() => { |
55 |
| - fakeAuth.signout(() => history.push('/')) |
56 |
| - }}><Text>Sign out</Text></TouchableHighlight> |
57 |
| - </View> |
58 |
| - ) : ( |
59 |
| - <Text>You are not logged in.</Text> |
60 |
| - ) |
61 |
| -)) |
62 |
| - |
63 |
| -const PrivateRoute = ({ component: Component, ...rest }) => ( |
64 |
| - <Route {...rest} render={props => ( |
| 52 | +const AuthButton = withRouter( |
| 53 | + ({ history }) => |
65 | 54 | fakeAuth.isAuthenticated ? (
|
66 |
| - <Component {...props}/> |
| 55 | + <View> |
| 56 | + <Text>Welcome!</Text> |
| 57 | + <TouchableHighlight |
| 58 | + style={styles.btn} |
| 59 | + underlayColor="#f0f4f7" |
| 60 | + onPress={() => { |
| 61 | + fakeAuth.signout(() => history.push("/")); |
| 62 | + }} |
| 63 | + > |
| 64 | + <Text>Sign out</Text> |
| 65 | + </TouchableHighlight> |
| 66 | + </View> |
67 | 67 | ) : (
|
68 |
| - <Redirect to={{ |
69 |
| - pathname: '/login', |
70 |
| - state: { from: props.location } |
71 |
| - }}/> |
| 68 | + <Text>You are not logged in.</Text> |
72 | 69 | )
|
73 |
| - )}/> |
74 |
| -) |
| 70 | +); |
75 | 71 |
|
76 |
| -const Public = () => <Text style={styles.header}>Public</Text> |
77 |
| -const Protected = () => <Text style={styles.header}>Protected</Text> |
| 72 | +function PrivateRoute({ component: Component, ...rest }) { |
| 73 | + return ( |
| 74 | + <Route |
| 75 | + {...rest} |
| 76 | + render={props => |
| 77 | + fakeAuth.isAuthenticated ? ( |
| 78 | + <Component {...props} /> |
| 79 | + ) : ( |
| 80 | + <Redirect |
| 81 | + to={{ |
| 82 | + pathname: "/login", |
| 83 | + state: { from: props.location } |
| 84 | + }} |
| 85 | + /> |
| 86 | + ) |
| 87 | + } |
| 88 | + /> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +function Public() { |
| 93 | + return <Text style={styles.header}>Public</Text>; |
| 94 | +} |
| 95 | + |
| 96 | +function Protected() { |
| 97 | + return <Text style={styles.header}>Protected</Text>; |
| 98 | +} |
78 | 99 |
|
79 | 100 | class Login extends Component {
|
80 |
| - state = { |
81 |
| - redirectToReferrer: false |
82 |
| - } |
| 101 | + state = { redirectToReferrer: false }; |
83 | 102 |
|
84 | 103 | login = () => {
|
85 | 104 | fakeAuth.authenticate(() => {
|
86 |
| - this.setState({ redirectToReferrer: true }) |
87 |
| - }) |
88 |
| - } |
| 105 | + this.setState({ redirectToReferrer: true }); |
| 106 | + }); |
| 107 | + }; |
89 | 108 |
|
90 | 109 | render() {
|
91 |
| - const { from } = this.props.location.state || { from: { pathname: '/' } } |
92 |
| - const { redirectToReferrer } = this.state |
| 110 | + const { from } = this.props.location.state || { from: { pathname: "/" } }; |
| 111 | + const { redirectToReferrer } = this.state; |
93 | 112 |
|
94 | 113 | if (redirectToReferrer) {
|
95 |
| - return ( |
96 |
| - <Redirect to={from}/> |
97 |
| - ) |
| 114 | + return <Redirect to={from} />; |
98 | 115 | }
|
99 | 116 |
|
100 | 117 | return (
|
101 | 118 | <View>
|
102 | 119 | <Text>You must log in to view the page at {from.pathname}</Text>
|
103 | 120 |
|
104 |
| - <TouchableHighlight style={styles.btn} underlayColor='#f0f4f7' onPress={this.login}> |
| 121 | + <TouchableHighlight |
| 122 | + style={styles.btn} |
| 123 | + underlayColor="#f0f4f7" |
| 124 | + onPress={this.login} |
| 125 | + > |
105 | 126 | <Text>Log in</Text>
|
106 | 127 | </TouchableHighlight>
|
107 | 128 | </View>
|
108 |
| - ) |
| 129 | + ); |
109 | 130 | }
|
110 | 131 | }
|
111 | 132 |
|
112 | 133 | const styles = StyleSheet.create({
|
113 | 134 | container: {
|
114 | 135 | marginTop: 25,
|
115 |
| - padding: 10, |
| 136 | + padding: 10 |
116 | 137 | },
|
117 | 138 | header: {
|
118 |
| - fontSize: 20, |
| 139 | + fontSize: 20 |
119 | 140 | },
|
120 | 141 | nav: {
|
121 |
| - flexDirection: 'row', |
122 |
| - justifyContent: 'space-around' |
| 142 | + flexDirection: "row", |
| 143 | + justifyContent: "space-around" |
123 | 144 | },
|
124 | 145 | navItem: {
|
125 | 146 | flex: 1,
|
126 |
| - alignItems: 'center', |
127 |
| - padding: 10, |
| 147 | + alignItems: "center", |
| 148 | + padding: 10 |
128 | 149 | },
|
129 | 150 | btn: {
|
130 | 151 | width: 200,
|
131 |
| - backgroundColor: '#E94949', |
132 |
| - justifyContent: 'center', |
133 |
| - alignItems: 'center', |
| 152 | + backgroundColor: "#E94949", |
| 153 | + justifyContent: "center", |
| 154 | + alignItems: "center", |
134 | 155 | padding: 10,
|
135 |
| - marginTop: 10, |
| 156 | + marginTop: 10 |
136 | 157 | }
|
137 |
| -}) |
| 158 | +}); |
138 | 159 |
|
139 |
| -export default AuthExample |
| 160 | +export default AuthExample; |
0 commit comments