-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthContext.js
48 lines (40 loc) · 1.23 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
import {createContext, useEffect, useState} from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {jwtDecode} from 'jwt-decode';
import 'core-js/stable/atob';
const AuthContext = createContext();
const AuthProvider = ({children}) => {
const [token, setToken] = useState('');
const [userId, setUserId] = useState('');
const [isLoading, setIsLoading] = useState('');
const [upcomingGames, setUpcomingGames] = useState([]);
const isLoggedIn = async () => {
try {
setIsLoading(true);
const userToken = await AsyncStorage.getItem('token');
setToken(userToken);
setIsLoading(false);
} catch (error) {
console.log('error', error);
}
};
useEffect(() => {
const fetchUser = async () => {
const token = await AsyncStorage.getItem('token');
const decodedToken = jwtDecode(token);
const userId = decodedToken.userId;
setUserId(userId);
};
fetchUser();
}, []);
useEffect(() => {
isLoggedIn();
}, [token]);
return (
<AuthContext.Provider
value={{token, isLoading, setToken, userId, setUserId,upcomingGames,setUpcomingGames}}>
{children}
</AuthContext.Provider>
);
};
export {AuthContext, AuthProvider};