-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
200 lines (180 loc) · 4.65 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// @flow
import React, {Component} from 'react';
import {AppRegistry, StyleSheet, Text, View, Alert, Button} from 'react-native';
import {
GoogleSignin,
GoogleSigninButton,
statusCodes,
} from '@react-native-community/google-signin';
import type {User} from '@react-native-community/google-signin';
import config from './config'; // see docs/CONTRIBUTING.md for details
import {TokenClearingView} from './TokenClearingView';
type ErrorWithCode = Error & {code?: string};
type State = {
error: ?ErrorWithCode,
userInfo: ?User,
};
class App extends Component<{}, State> {
state = {
userInfo: null,
error: null,
};
async componentDidMount() {
this._configureGoogleSignIn();
await this._getCurrentUser();
}
_configureGoogleSignIn() {
GoogleSignin.configure({
webClientId: config.webClientId,
offlineAccess: false,
});
}
async _getCurrentUser() {
try {
const userInfo = await GoogleSignin.signInSilently();
this.setState({userInfo, error: null});
} catch (error) {
const errorMessage =
error.code === statusCodes.SIGN_IN_REQUIRED
? 'Please sign in :)'
: error.message;
this.setState({
error: new Error(errorMessage),
});
}
}
render() {
const {userInfo} = this.state;
const body = userInfo
? this.renderUserInfo(userInfo)
: this.renderSignInButton();
return (
<View style={[styles.container, {flex: 1}]}>
{this.renderIsSignedIn()}
{this.renderGetCurrentUser()}
{this.renderGetTokens()}
{body}
</View>
);
}
renderIsSignedIn() {
return (
<Button
onPress={async () => {
const isSignedIn = await GoogleSignin.isSignedIn();
Alert.alert(String(isSignedIn));
}}
title="is user signed in?"
/>
);
}
renderGetCurrentUser() {
return (
<Button
onPress={async () => {
const userInfo = await GoogleSignin.getCurrentUser();
Alert.alert(
'current user',
userInfo ? JSON.stringify(userInfo.user) : 'null',
);
}}
title="get current user"
/>
);
}
renderGetTokens() {
return (
<Button
onPress={async () => {
const isSignedIn = await GoogleSignin.getTokens();
Alert.alert('tokens', JSON.stringify(isSignedIn));
}}
title="get tokens"
/>
);
}
renderUserInfo(userInfo) {
return (
<View style={styles.container}>
<Text style={{fontSize: 18, fontWeight: 'bold', marginBottom: 20}}>
Welcome {userInfo.user.name}
</Text>
<Text>Your user info: {JSON.stringify(userInfo.user)}</Text>
<TokenClearingView userInfo={userInfo} />
<Button onPress={this._signOut} title="Log out" />
{this.renderError()}
</View>
);
}
renderSignInButton() {
return (
<View style={styles.container}>
<GoogleSigninButton
size={GoogleSigninButton.Size.Standard}
color={GoogleSigninButton.Color.Auto}
onPress={this._signIn}
/>
{this.renderError()}
</View>
);
}
renderError() {
const {error} = this.state;
if (!error) {
return null;
}
const text = `${error.toString()} ${error.code ? error.code : ''}`;
return <Text>{text}</Text>;
}
_signIn = async () => {
try {
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
this.setState({userInfo, error: null});
} catch (error) {
if (error.code === statusCodes.SIGN_IN_CANCELLED) {
// sign in was cancelled
Alert.alert('cancelled');
} else if (error.code === statusCodes.IN_PROGRESS) {
// operation in progress already
Alert.alert('in progress');
} else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
Alert.alert('play services not available or outdated');
} else {
Alert.alert('Something went wrong', error.toString());
this.setState({
error,
});
}
}
};
_signOut = async () => {
try {
await GoogleSignin.revokeAccess();
await GoogleSignin.signOut();
this.setState({userInfo: null, error: null});
} catch (error) {
this.setState({
error,
});
}
};
}
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
export default App;