-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInfoContext.js
More file actions
89 lines (80 loc) · 2.1 KB
/
UserInfoContext.js
File metadata and controls
89 lines (80 loc) · 2.1 KB
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
import AsyncStorage from '@react-native-async-storage/async-storage';
import React, {createContext, useState, useContext} from 'react';
const UserInfoContext = createContext({
token: null,
name: null,
email: null,
id: null,
storeToken: () => {},
clearToken: () => {},
storeName: () => {},
storeEmail: () => {},
storeId: () => {},
});
export const useUserInfo = () => useContext(UserInfoContext);
export const UserInfoProvider = ({children}) => {
const [token, setToken] = useState(null);
const [name, setName] = useState(null);
const [email, setEmail] = useState(null);
const [id, setId] = useState(null);
const storeToken = async newToken => {
try {
await AsyncStorage.setItem('userToken', newToken);
setToken(newToken);
} catch (e) {
console.log('토큰 저장 중 에러 발생', e);
}
};
// 토큰을 초기화하는 함수
const clearToken = async () => {
try {
await AsyncStorage.removeItem('userToken'); // AsyncStorage에서 토큰 제거
setToken(null); // 상태를 null로 설정
} catch (e) {
console.log('토큰 초기화 중 에러 발생', e);
}
};
// 이름 저장
const storeName = async newName => {
try {
await AsyncStorage.setItem('userName', newName);
setName(newName);
} catch (e) {
console.log('이름 저장 중 에러 발생', e);
}
};
// 이메일 저장
const storeEmail = async newEmail => {
try {
await AsyncStorage.setItem('userEmail', newEmail);
setEmail(newEmail);
} catch (e) {
console.log('이메일 저장 중 에러 발생', e);
}
};
// 아이디 저장
const storeId = async newId => {
try {
await AsyncStorage.setItem('userId', newId);
setId(newId);
} catch (e) {
console.log('아이디 저장 중 에러 발생', e);
}
};
return (
<UserInfoContext.Provider
value={{
token,
name,
email,
id,
storeToken,
clearToken,
storeName,
storeEmail,
storeId,
}}>
{children}
</UserInfoContext.Provider>
);
};