-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthContext.js
53 lines (44 loc) · 1.19 KB
/
AuthContext.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
import AsyncStorage from '@react-native-async-storage/async-storage';
import {jwtDecode} from 'jwt-decode';
import {createContext, useEffect, useState} from 'react';
const AuthContext = createContext();
const AuthProvider = ({children}) => {
const [token, setToken] = useState('');
const [userId, setUserId] = useState('');
const [userInfo,setUserInfo] = useState(null);
const [upcomingGames, setUpComingGames] = useState([]);
const isLoggedIn = async () => {
try {
const userToken = await AsyncStorage.getItem('authToken');
setToken(userToken);
} catch (error) {
console.log('error', error);
}
};
useEffect(() => {
const fetchUser = async () => {
const token = await AsyncStorage.getItem('authToken');
const decodedToken = jwtDecode(token);
const userId = decodedToken.userId;
setUserId(userId);
};
fetchUser();
}, []);
useEffect(() => {
isLoggedIn();
}, []);
return (
<AuthContext.Provider
value={{
token,
setToken,
userId,
setUserId,
userInfo,
setUserInfo,
}}>
{children}
</AuthContext.Provider>
);
};
export {AuthContext, AuthProvider};