forked from AkshayWarrier/Volunteer-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignInEmailOption.js
151 lines (131 loc) · 4.39 KB
/
SignInEmailOption.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
import { StyleSheet, Text, View, TouchableOpacity, TextInput, ScrollView, Image } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import { StatusBar } from 'expo-status-bar';
import { signInWithPopup,GoogleAuthProvider,signInWithCredential } from "firebase/auth"
import { users_collection,organisations_collection,auth,provider,query_db, isNewUser } from "./methods.js";
import * as Google from 'expo-auth-session/providers/google';
import { useEffect,useState } from 'react';
export const SignInEmailOption = ({ setIsOrganisation,setIsLogged,setIsSigned,setIsGoogleAuth,setUserEmail }) => {
const navigation = useNavigation();
const [request, response, promptAsync] = Google.useIdTokenAuthRequest({
clientId: '51363481835-ofuhhpcteqcm2rod61bs6rf3rhfjkbrf.apps.googleusercontent.com',
androidClientId: '133007557332-eqn7ohskg2u004ceknkdtn5pk7kvava4.apps.googleusercontent.com'
});
const [arr,setArr] = useState([]);
useEffect(() => {
if (response?.type === 'success') {
const {id_token} = response.params;
const credential = GoogleAuthProvider.credential(id_token);
signInWithCredential(auth,credential).then((auth_result)=>{
const user = auth_result.user;
const email = user.email;
const displayName = user.displayName;
// Create a query against the collection and wait for the query to complete.
const user_query = query_db("Email", "==", email,users_collection);
const org_query = query_db("Email", "==", email,organisations_collection);
setUserEmail(email);
return Promise.all([user_query,org_query]).then((values)=>{
const user_query = values[0];
const org_query = values[1];
if(user_query.empty && org_query.empty){
setArr(["new","Signup"]);
}
else{
if(!org_query.empty){
setArr([true,"OrganizationFeed"]);
}
else{
setArr([false,"Feed"]);
}
}
}
)
})
}
}, [response]);
useEffect(() => {
if(arr.length>0){
setIsSigned(true);
setIsGoogleAuth(true);
if(arr[0]!="new"){
setIsOrganisation(arr[0]);
setIsLogged(true);
navigation.navigate(arr[1]);
}
else{
console.log("No user found");
navigation.navigate("VolunteerOptions");
}
}
}, [arr]);
// const navigation = useNavigation();
return (
<View style={styles.container}>
<View>
<TouchableOpacity style={styles.button}
onPress={() => navigation.navigate("Login")}
>
<Text style={styles.text}>Continue with Email</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.googleButton}
onPress={()=>{
promptAsync();
}}
>
<Image source={require('./assets/images/google_logo.png')} style={styles.googleImage} />
<Text style={styles.googleText}>Continue with Google</Text>
</TouchableOpacity>
<StatusBar style="auto" />
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#F7FFF7",
alignItems: 'center',
justifyContent: 'center',
alignContent: 'center',
},
button: {
// marginTop: 160,
backgroundColor: "#1A535C",
width: 300,
height: 50,
padding: 10,
alignItems: 'center',
justifyContent: 'center',
},
text: {
color: "#F7FFF7",
fontSize: 16,
fontFamily: 'Poppins',
},
googleButton: {
display: 'flex',
flexDirection: 'row',
backgroundColor: "#F7FFF7",
width: 300,
height: 50,
padding: 10,
marginTop: 50,
marginBottom: 50,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 2,
borderColor: "#1A535C",
},
googleText: {
flex: 3,
color: "#1A535C",
fontSize: 16,
fontFamily: 'Poppins',
},
googleImage: {
width: 25,
height: 25,
marginRight: 30,
marginLeft: 10,
},
});