· 4 years ago · Feb 10, 2021, 08:42 AM
1const authContext = React.useMemo(
2 () => ({
3 signIn: async data => {
4 // In a production app, we need to send some data (usually username, password) to server and get a token
5 // We will also need to handle errors if sign in failed
6 // After getting token, we need to persist the token using `AsyncStorage`
7 // In the example, we'll use a dummy token
8
9 state.isLoading = true;
10
11 /*
12 if(state.isLoading == true){
13 console.log(state.isLoading);
14 return(
15 <SafeAreaView style={{flex:1,justifyContent:'center',alignItems:'center'}}>
16 <ActivityIndicator size="large"/>
17 </SafeAreaView>
18 )
19 }*/
20 console.log("Email: ", data['email']);
21 console.log("Password: ", data['password']);
22
23 secretKey = '**********************************';
24
25 useEffect(() => {
26 fetch('https://website.it/wp-content/themes/theme/app/login.php?key=' + secretKey, {
27 method: 'POST',
28 headers: {
29 'Accept': 'application/json',
30 'Content-Type': 'application/json',
31 },
32 body: JSON.stringify({
33
34 email: data['email'],
35
36 password: data['password'],
37
38 })
39 }).then((response) => response.json()).then((responseJson) => {
40 // If server response message same as Data Matched
41 if(responseJson === 'Data Matched'){
42 //Then open Profile activity and send user email to profile activity.
43 //navigation.navigate('Home', { Email: email });
44 state.isLoading = false;
45 dispatch({ type: 'SIGN_IN', token: 'dummy-auth-token' });
46 }else{
47 state.isLoading = false;
48 Alert.alert(responseJson);
49 }
50 }).catch((error) => {
51 console.error(error);
52 });
53 }, []);
54
55 //setLoading(false);
56
57 //dispatch({ type: 'SIGN_IN', token: 'dummy-auth-token' });
58 },