-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuthContext.js
48 lines (41 loc) · 1.13 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 React, { createContext, useContext, useState } from 'react';
import { AsyncStorage } from 'react-native';
export const AuthContext = createContext();
export const AuthProvider = ({ isLoggedIn: isLoggedInProp, children }) => {
const [isLoggedIn, setIsLoggedIn] = useState(isLoggedInProp);
const logUserIn = async token => {
// console.log(token);
try {
await AsyncStorage.setItem('isLoggedIn', 'true');
await AsyncStorage.setItem('jwt', token);
setIsLoggedIn(true);
} catch (error) {
console.log(error);
}
};
const logUserOut = async () => {
try {
await AsyncStorage.setItem('isLoggedIn', 'false');
setIsLoggedIn(false);
} catch (error) {
console.log(error);
}
};
return (
<AuthContext.Provider value={{ isLoggedIn, logUserIn, logUserOut }}>
{children}
</AuthContext.Provider>
);
};
export const useIsLoggedIn = () => {
const { isLoggedIn } = useContext(AuthContext);
return isLoggedIn;
};
export const useLogIn = () => {
const { logUserIn } = useContext(AuthContext);
return logUserIn;
};
export const useLogOut = () => {
const { logUserOut } = useContext(AuthContext);
return logUserOut;
};