-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
129 lines (116 loc) · 3.12 KB
/
App.js
File metadata and controls
129 lines (116 loc) · 3.12 KB
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
import React, { Component} from 'react';
import { Button, StyleSheet, StatusBar, ActivityIndicator, Text, View, TouchableOpacity, ToastAndroid, Platform } from 'react-native';
import { GoogleSignin, GoogleSigninButton } from 'react-native-google-signin';
import WEBCLIENT_ID from './gconfig';
import { createStackNavigator, createSwitchNavigator } from 'react-navigation';
import HomeScreen from './components/HomeScreen';
class SignInScreen extends React.Component {
_signIn = async () => {
try {
const user = await GoogleSignin.signIn();
this.props.navigation.navigate('App');
} catch (error) {
console.log(error);
if (error.code === 'CANCELED') {
error.message = 'user canceled the login flow';
}
ToastAndroid.show(error.message, ToastAndroid.SHORT);
}
};
_signOut = async () => {
try {
await GoogleSignin.revokeAccess();
await GoogleSignin.signOut();
this.setState({ user: null });
} catch (error) {
this.setState({
error,
});
}
};
render() {
return (
<View style={styles.container}>
<GoogleSigninButton
style={{ width: 230, height: 48 }}
size={GoogleSigninButton.Size.Standard}
color={GoogleSigninButton.Color.Auto}
onPress={this._signIn}
/>
</View>
);
}
}
class AuthLoadingScreen extends React.Component {
constructor(props) {
super(props);
}
async componentDidMount(){
await this._configureGoogleSignIn();
await this._getCurrentUser();
}
async _configureGoogleSignIn() {
await GoogleSignin.hasPlayServices({ autoResolve: true });
const configPlatform = {
...Platform.select({
ios: {},
android: {},
}),
};
await GoogleSignin.configure({
...configPlatform,
webClientId: WEBCLIENT_ID,
offlineAccess: false,
scopes: ['https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/youtube','https://www.googleapis.com/auth/youtube.force-ssl']
});
}
async _getCurrentUser() {
try {
const user = await GoogleSignin.currentUserAsync();
console.log(user);
this.props.navigation.navigate(user ? 'App' : 'Auth');
} catch (error) {
this.setState({
error,
});
}
}
render() {
return (
<View style={styles.container}>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
}
const AppStack = createStackNavigator({ Home: HomeScreen }, { headerMode: 'none',headerTitle: 'Home'} );
const AuthStack = createStackNavigator({ SignIn: SignInScreen},{ headerMode: 'none'});
export default createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
App: AppStack,
Auth: AuthStack,
},
{
initialRouteName: 'AuthLoading',
}
);
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#6200EE',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});