-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapp.js
160 lines (149 loc) · 3.78 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React, { Component } from 'react';
import {
Image,
Linking,
StyleSheet,
Platform,
Text,
View
} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
import SafariView from 'react-native-safari-view';
export default class App extends Component {
state = {
user: undefined, // user has not logged in yet
};
// Set up Linking
componentDidMount() {
// Add event listener to handle OAuthLogin:// URLs
Linking.addEventListener('url', this.handleOpenURL);
// Launched from an external URL
Linking.getInitialURL().then((url) => {
if (url) {
this.handleOpenURL({ url });
}
});
};
componentWillUnmount() {
// Remove event listener
Linking.removeEventListener('url', this.handleOpenURL);
};
handleOpenURL = ({ url }) => {
// Extract stringified user string out of the URL
const [, user_string] = url.match(/user=([^#]+)/);
this.setState({
// Decode the user string and parse it into JSON
user: JSON.parse(decodeURI(user_string))
});
if (Platform.OS === 'ios') {
SafariView.dismiss();
}
};
// Handle Login with Facebook button tap
loginWithFacebook = () => this.openURL('http://localhost:3000/auth/facebook');
// Handle Login with Google button tap
loginWithGoogle = () => this.openURL('http://localhost:3000/auth/google');
// Open URL in a browser
openURL = (url) => {
// Use SafariView on iOS
if (Platform.OS === 'ios') {
SafariView.show({
url: url,
fromBottom: true,
});
}
// Or Linking.openURL on Android
else {
Linking.openURL(url);
}
};
render() {
const { user } = this.state;
return (
<View style={styles.container}>
{ user
? // Show user info if already logged in
<View style={styles.content}>
<Text style={styles.header}>
Welcome {user.name}!
</Text>
<View style={styles.avatar}>
<Image source={{ uri: user.avatar }} style={styles.avatarImage} />
</View>
</View>
: // Show Please log in message if not
<View style={styles.content}>
<Text style={styles.header}>
Welcome Stranger!
</Text>
<View style={styles.avatar}>
<Icon name="user-circle" size={100} color="rgba(0,0,0,.09)" />
</View>
<Text style={styles.text}>
Please log in to continue {'\n'}
to the awesomness
</Text>
</View>
}
{/* Login buttons */}
<View style={styles.buttons}>
<Icon.Button
name="facebook"
backgroundColor="#3b5998"
onPress={this.loginWithFacebook}
{...iconStyles}
>
Login with Facebook
</Icon.Button>
<Icon.Button
name="google"
backgroundColor="#DD4B39"
onPress={this.loginWithGoogle}
{...iconStyles}
>
Or with Google
</Icon.Button>
</View>
</View>
);
}
}
const iconStyles = {
borderRadius: 10,
iconStyle: { paddingVertical: 5 },
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#FFF',
},
content: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
avatar: {
margin: 20,
},
avatarImage: {
borderRadius: 50,
height: 100,
width: 100,
},
header: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
text: {
textAlign: 'center',
color: '#333',
marginBottom: 5,
},
buttons: {
justifyContent: 'space-between',
flexDirection: 'row',
margin: 20,
marginBottom: 30,
},
});