forked from shreyaa-1/AAM-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
128 lines (124 loc) · 3.51 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
import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import Tabs from './navigation/tabs';
import messaging from '@react-native-firebase/messaging';
import React, {useEffect, useMemo, useReducer, useState} from 'react';
import firestore from '@react-native-firebase/firestore';
import Login from './screens/login';
import NotiComp from './screens/notiComp';
import {AuthContext} from './screens/context';
import AsyncStorage from '@react-native-async-storage/async-storage';
import axios from 'axios';
import SplashScreen from './screens/splashscreen';
const Stack = createNativeStackNavigator();
const App = () => {
const initialLoginState = {
username: null,
userToken: null,
ftoken: null,
};
const [firetoken, setFiretoken] = useState('');
const loginReducer = (prevState, action) => {
switch (action.type) {
case 'RETRIVE_TOKEN':
return {
...prevState,
userToken: action.token,
};
case 'LOGIN':
console.log(action);
return {
...prevState,
username: action.id,
userToken: action.token,
};
case 'LOGOUT':
return {
...prevState,
username: null,
userToken: null,
};
}
};
const [loginState, dispatch] = useReducer(loginReducer, initialLoginState);
const authContext = useMemo(() => ({
login: async (email, password) => {
try {
const data = await axios.post(
'https://0842-203-110-242-41.in.ngrok.io/loginn',
{
email: email,
dob: password,
},
);
console.log(data.data[0]);
if (data.data) {
await AsyncStorage.setItem(
'userToken',
JSON.stringify({
userToken: data.data[0].email,
username: data.data[0].email,
}),
);
dispatch({
type: 'LOGIN',
id: data.data[0].email,
token: data.data[0].email,
});
console.log('LOginState', loginState);
}
} catch (err) {
console.log(err);
}
},
logOut: async () => {
try {
await AsyncStorage.removeItem('userToken');
dispatch({type: 'LOGOUT'});
} catch (err) {
console.log(err);
}
},
}));
useEffect(() => {
const timeOut = setTimeout(async () => {
let userToken1 = null;
let fToken1 = null;
try {
await AsyncStorage.getItem('userToken').then(res => {
userToken1 = res;
if (userToken1) {
const {userToken} = JSON.parse(userToken1);
dispatch({type: 'RETRIVE_TOKEN', token: userToken});
}
});
// await checktoken();
} catch (err) {
console.log(err);
}
return () => {
clearTimeout(timeOut);
};
}, 1000);
}, []);
// useEffect(() => {
//console.log('sdhaisd');
// console.log(loginState.ftoken, 'sas');
//rr }, []);
return (
<AuthContext.Provider value={authContext}>
<NotiComp />
<NavigationContainer>
<Stack.Navigator>
{loginState.userToken != null ? (
<Stack.Screen name="Annual Alumni Meet" component={Tabs} />
) : (
// console.log(loginState)
<Stack.Screen name="login" component={Login} />
)}
</Stack.Navigator>
</NavigationContainer>
</AuthContext.Provider>
);
};
export default App;