-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountScreen.js
More file actions
239 lines (224 loc) · 6.62 KB
/
AccountScreen.js
File metadata and controls
239 lines (224 loc) · 6.62 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, StyleSheet, Image, Button, TouchableOpacity } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Toast from 'react-native-simple-toast';
import url from './url';
import MyButton from './MyButton';
const AccountScreen = () => {
const [loginStatus, setLoginStatus] = useState(0);
const [id, setID] = useState('');
const [pwd, setPWD] = useState('');
const [userInfo, setUserInfo] = useState({});
const update = async() => {
await fetch(url.clearCookie);
try {
const res_islg = await fetch(url.isLoggedIn, {
method: 'GET',
// credentials: 'include',
headers: {
'Cookie': await AsyncStorage.getItem('@cookie')
}
});
const data_islg = await res_islg.json();
if (data_islg.ok == 1 && data_islg.result == 'yes') {
setLoginStatus(1);
try { //get user info
const res_uinfo = await fetch(url.getUserInfo, {
method: 'GET',
headers: {
'Cookie': await AsyncStorage.getItem('@cookie')
}
});
const data_uinfo = await res_uinfo.json();
if (data_uinfo.ok == 1) {
setUserInfo(data_uinfo.result);
}
} catch (err) {
console.error(err);
}
} else {
setLoginStatus(0);
setUserInfo({});
}
} catch (err) {
console.error(err);
}
};
useEffect(() => {
const prep = async() => {
update();
};
prep();
}, []);
const login = async() => {
let errorCode = '-1';
try {
const res_newid = await fetch(url.isIDNew+`?id=${id}`);
const data_newid = await res_newid.json();
if (data_newid.ok == 1) {
if (data_newid.result == 'yes') {
// Register
const res_register = await fetch(url.register, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: id,
password: pwd
})
});
const data_register = await res_register.json();
if (data_register.ok == 1) {
console.log('Registered...');
Toast.show('Registered👌');
login();
} else {
// Register failed
errorCode = '3';
Toast.show(`Registration Failed: ${data_register.msg}❌`);
}
} else {
// Login
const res_login = await fetch(url.login, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: id,
password: pwd
})
});
const data_login = await res_login.json();
if (data_login.ok == 1) {
const cookie = res_login.headers.get('Set-Cookie');
await AsyncStorage.setItem('@cookie', cookie);
console.log(`Cookie[${cookie}] has been saved locally...`);
console.log('Logged In');
Toast.show('Logged In🎉');
// other tasks
setID(''); //clear inputs
setPWD('');
await fetch(url.clearCookie); //clear real cookie, avoid it being sent to server in the future
await update(); //update login status
} else {
// Login failed
errorCode = '4';
Toast.show(`Login Failed: ${data_login.msg}❌`);
}
}
} else {
errorCode = '2';
Toast.show(`Error #${errorCode}: ${data_newid.msg}❌`);
}
} catch (err) {
console.error(err);
errorCode = '1';
Toast.show(`Network Error🔗❌`);
}
};
const logout = async() => {
await fetch(url.logout);
await AsyncStorage.removeItem('@cookie');
//userInfo will be wiped automatically in update()
Toast.show('Logged Out👌');
await update();
}
return(
<View>
{(loginStatus == 1) ?
<View style={styles.individual_container}>
<View style={styles.loggedin_symbol_container}><Text style={styles.loggedin_symbol}>✅</Text></View>
<View>
<Text style={styles.loggedin_text}>You're Logged In as
<Text style={styles.loggedin_text_name}> @{userInfo.ID}</Text>
</Text>
</View>
<View style={styles.logout_button_container}>
<MyButton title='Logout' onPress={() => logout()} />
</View>
</View>
:
<View style={styles.individual_container}>
<View style={styles.title_all_container}>
<View><Text style={styles.title_text}>Login to Your Account</Text></View>
<View><Text style={styles.title_note_text}>(New users are automatically registered)</Text></View>
</View>
<View style={styles.input_all_container}>
<View style={styles.input_labels_container}>
<View><Text style={styles.input_labels}>ID</Text></View>
<View><Text style={styles.input_labels}>Password</Text></View>
</View>
<View style={styles.input_boxs_container}>
<View><TextInput style={styles.input_boxs} value={id} onChangeText={(t) => setID(t)} placeholder='🆔'/></View>
<View><TextInput style={styles.input_boxs} value={pwd} onChangeText={(t) => setPWD(t)} secureTextEntry={true} placeholder='🔑'/></View>
</View>
</View>
<View>
<MyButton title='Login/Register' onPress={() => login()} />
</View>
</View>}
</View>
);
};
const styles = StyleSheet.create({
individual_container: {
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center'
},
title_all_container: {
alignItems: 'center'
},
title_text: {
color: 'darkslategray',
fontSize: 27.5,
},
title_note_text: {
color: 'gray',
marginTop: 3,
fontSize: 13
},
input_all_container: {
flexDirection: 'row',
marginTop: 10
},
input_labels_container: {
marginRight: 6
},
input_boxs_container: {
},
input_labels: {
textAlign: 'right',
color: 'dimgray',
fontSize: 19
},
input_boxs: {
fontSize: 19,
color: 'dimgray',
width: 90,
textAlign: 'center'
},
loggedin_symbol_container: {
margin: 10
},
loggedin_symbol: {
marginTop: 15,
fontSize: 45
},
loggedin_text: {
fontSize: 19,
color: 'gray',
},
loggedin_text_name: {
fontSize: 21,
color: 'slategray',
fontWeight: 'bold'
},
logout_button_container: {
marginTop: 10
}
});
export default AccountScreen;