This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
464 lines (394 loc) · 16.2 KB
/
App.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//App.js
//--^--
//
/* This is first screen - Login Screen */
/* importing required modules */
import React, {useState} from 'react';
import { Text, View, TextInput, Platform, Image, KeyboardAvoidingView, TouchableOpacity, TouchableHighlight, StyleSheet, YellowBox, Keyboard} from 'react-native';
import { Appearance, AppearanceProvider, useColorScheme } from 'react-native-appearance';
YellowBox.ignoreWarnings(['RootErrorBoundary']);
var theme = false
//variable for screen dimensions if needed
/*importing Button Images */
import Show_Pass from './assets/Show_Pass.png';
import User_Check_Light from './assets/User_Check_Light.png';
import User_Check_Dark from './assets/User_Check_Dark.png'
import Hide_Pass_Dark from './assets/Hide_Pass_Dark.png';
import Hide_Pass_Light from './assets/Hide_Pass_Light.png'
import Verified_User_Light from './assets/Verified_User_Light.png';
import Verified_User_Dark from './assets/Verified_User_Dark.png'
/* importing components*/
import UserHome from './userpages/UserHome';
import SignUp from './SignUp';
/* importing list of users*/
import Users from './users.json'
var z = 0
/*main Function */
export default function UserLogin() {
/*list of Messages */
var messages = ["seems you're not registered with us!", "Please enter a userid address first!", "Too Many Failed attempts, Please Try Again in "]
/* Unused Variables for future Usage*
let [goBack, setGoBack] = useState(false);
*/
// setting home theme same as device
let colorScheme = Appearance.getColorScheme();
theme=(colorScheme==='dark')
/*List of State Variables*/
let [count, setCount] = useState(0); //counter for unauthorised login attempt
let [result, setResult] = useState(''); //login attempt messages [if needed]
let [useridCheckResult, setUseridCheckResult] = useState(User_Check); //cmage URI source for userid check
let [disableUserid, setDisableUserid] = useState(false); //disables userid input if counter exceeds
let [useridCheck, setUseridCheck] = useState(true); //checks if Userid is registered or not
let [passBoxVisibility, setPassBoxVisibility] = useState('none'); //enables password box in case userid is registered
let [registeredUserid, setRegisteredUserid] = useState('none'); //enables visibility of login messages if needed.
let [viewPassword, setViewPassword] = useState(Hide_Pass); //image URI source for Password View
let [goHome, setGoHome] = useState(false); //goes to Home Screen if true
let [userid, setUserId] = useState(''); //sets userid if entered id is valid
let [signUp, setSignUp] = useState(false); //allows signing up
let [dark, setDark] = useState(theme); //sets theme for userlogin
//set theme specific icons
let Hide_Pass = dark?Hide_Pass_Dark:Hide_Pass_Light
let User_Check = dark?User_Check_Dark:User_Check_Light
let Verified_User = dark?Verified_User_Dark:Verified_User_Light
const [useridEntered, setUseridEntered] = useState(''); //takes Input for userid
const [passwordEntered, setPasswordEntered] = useState(''); //takes Input for password
const [b, setB] = useState(5); //for timer in case of repeated failed attempts
/* changing theme */
function change_theme() {
setDark(!dark)
Hide_Pass = !dark?Hide_Pass_Dark:Hide_Pass_Light
User_Check = !dark?User_Check_Dark:User_Check_Light
Verified_User=!dark?Verified_User_Dark:Verified_User_Light
setViewPassword(viewPassword==Show_Pass?Show_Pass:Hide_Pass)
setUseridCheckResult((useridCheckResult==Verified_User_Dark||useridCheckResult==Verified_User_Light)?Verified_User:User_Check)
}
/* verifying userid */
function userid_check() {
setUseridCheck(false);
if (useridEntered in Users) { //userid is true
setUserId(useridEntered);
setRegisteredUserid('none');
setPassBoxVisibility('flex');
setUseridCheckResult(Verified_User);
setCount(0);
setB(5);
z = 1;
}
if (z==0) { //userid not registered
setRegisteredUserid('flex');
setCount(count + 1);
setResult(messages[0])
setPassBoxVisibility('none');
setUseridCheckResult(User_Check_Light);
}
if (count > 2) {
setDisableUserid(true);
setTimeout(setUseridCheck, b*1000, true);
setTimeout(setDisableUserid, b*1000, false);
setTimeout(setResult, b*1000, '');
setB(b*2);
setTimeout(setCount, b*1000, 0);
setResult(messages[2] + b + '\xa0seconds!');
}
}
/*end of Userid verification*/
/* password Verification */
function login_attempt() {
// alert(userid)
if (passwordEntered==Users[userid].pwd) {
setGoHome(true);
} else {
alert('Nope!');
}
}
/*end of Password Verification */
/* user requesting sign up*/
if (signUp) {
var id=''
if (result==messages[0]) id=useridEntered
return (
<View style={{flex:1}}>
<SignUp theme={dark} id={id} setTheme={change_theme} goHome={setGoHome} setUserId={setUserId} signUp={reset_defaults} />
</View>
)
}
/*home Screen */
if (goHome) {
// alert('Welcome ' + Users[userid].name+'!')
return(
<View style={{flex:1}}>
<UserHome close={reset_defaults} id={userid}/>
</View>
)
}
/* password visibility setting */
function password_visibility() {
if (viewPassword!=Hide_Pass) {
setViewPassword(Hide_Pass);
} else {
setViewPassword(Show_Pass);
}
}
/* reset passoword fields and hide password box (Invoked when userid is changed)*/
function reset_fields() {
setPasswordEntered('');
setPassBoxVisibility('none');
setUseridCheckResult(User_Check);
setUseridCheck(true);
setResult('');
setGoHome(false);
setViewPassword(Hide_Pass);
}
/*default reset invoked when user comes back from signup page or signed in user logs out or deleted their account*/
function reset_defaults() {
setGoHome(false);
setSignUp(false);
setUseridEntered('');
setPasswordEntered('');
setPassBoxVisibility('none');
setUseridCheckResult(User_Check);
setUseridCheck(true);
setResult('');
setViewPassword(Hide_Pass);
}
/*main Return function */
return (
// A - main Container
<KeyboardAvoidingView style={[styles.mainbox, {backgroundColor:dark?'#333333':'#ffdddd'}]} behavior={Platform.OS == "ios" ? "padding" : ""}>
{/* A - 1 - body (Contain login box)*/}
<View style={styles.bodybox}>
{/* A - 1 - A - contains login elements */}
<View style={[styles.mainloginbox, {backgroundColor:dark?'#777777':'#ddffff'}]}>
{/* A - 1 - A - 1 - login Header*/}
<View style={[styles.headerlogin, {backgroundColor:dark?'#333333':'#367588'}]}>
{/* A - 1 - A - 1 - A - login header text */}
<Text onPress={Keyboard.dismiss} style={styles.headerlogintext}>let's get you signed in!</Text>
</View>
{/* A - 1 - A - 2 - userid Main Box*/}
<View style={[styles.useridbox, {backgroundColor:dark?'#666666':'#ffffdd'}]}>
{/* A - 1 - A - 2 - A - userid Input (can be disabled)*/}
<TextInput textContentType="username" defaultValue={useridEntered}
onEndEditing={()=>userid_check()} returnKeyType='go' placeholderTextColor='gray'
style={[styles.inputuserid, {color:dark?'#ffffff':'#000000'}]}
autoCapitalize='none' placeholder={'Please enter your user id...'}
onChangeText={(useridEntered)=>setUseridEntered(useridEntered)}
editable={!disableUserid} enablesReturnKeyAutomatically={true}
onChange={()=>reset_fields()} keyboardAppearance={dark?'dark':'light'}
/>
{/* A - 1 - A - 2 - B - userid check button [userid_check] (with disable and variable image uri)*/}
<TouchableHighlight activeOpacity={1}
disabled={!useridCheck} underlayColor="#0070bb"
onPress={userid_check} style={styles.useridverify}>
{/* A - 1 - A - 2 - B - 1 - userid check button icon*/}
<Image style={styles.buttonthumbs} source={(useridCheckResult==null?dark?User_Check_Dark:User_Check_Light:useridCheckResult)} />
</TouchableHighlight>
</View>
{/* A - 1 - A - 3 - text returning error messages in userid verification*/}
<Text style={[styles.resulttext, { display:registeredUserid}]}>{result}</Text>
{/* A - 1 - A - 4 - container used for hiding passwordBox until userid is verified*/}
<View style={{display:passBoxVisibility, alignItems:'center'}}>
{/* A - 1 - A - 4 - A - flexbox for password*/}
<View style={[styles.passwordbox, {backgroundColor:dark?'black':'lightgrey'}]}>
{/* A - 1 - A - 4 - A - 1 - password Input Box*/}
<TextInput secureTextEntry={viewPassword==(dark?Hide_Pass_Dark:Hide_Pass_Light)} style={[styles.inputpassword, {color:dark?'white':'black'}]}
placeholder={'Input Your Password Here!'} placeholderTextColor='grey'
onChangeText={passwordEntered => setPasswordEntered(passwordEntered)}
defaultValue={passwordEntered} returnKeyType='done' keyboardAppearance={dark?'dark':'light'}
textContentType="password" autoCompleteType='off'
/>
{/* A - 1 - A - 4 - A - 2 - button for viewing password [password_visibility] (has a variable image uri)*/}
<TouchableOpacity
activeOpacity={1}underlayColor={'#747474'}
onPress={password_visibility} style={styles.passwordview}>
{/* A - 1 - A - 4 - A - 2 - A - icon for password view button */}
<Image style={styles.buttonthumbs} source={viewPassword} />
</TouchableOpacity>
</View>
{/* A - 1 - A - 4 - B - button to submit password [login_attempt] (shares the visibility criteria same as password box)*/}
<TouchableOpacity
style={[styles.submitbutton, {backgroundColor:dark?'#444444':'#000044'}]}
underlayColor="#000" onPress={login_attempt}>
{/* A - 1 - A - 4 - B - 1 - password submit text */}
<Text style={styles.submitbuttontext}>submit</Text>
</TouchableOpacity>
</View>
{/* A - 1 - A - 5 - button for signing up if the user is not registered and is interested []*/}
<View style={[styles.signupbox, {backgroundColor:dark?'#777777':'#C9C9C9'}]}>
{/* A - 1 - A - 5 - A - text for sign up */}
<Text on onPress={Keyboard.dismiss}>Don't have any account with us yet?</Text>
{/* A - 1 - A - 5 - B - signup button */}
<TouchableOpacity style={styles.signupbutton} onPress={() => setSignUp(true)}>
{/* A - 1 - A - 5 - B - 1 - signup button text*/}
<Text style={[styles.signupbuttontext, {color:dark?'#ffffdd':'darkred'}]}>sign up!</Text>
</TouchableOpacity>
</View>
</View>
</View>
{/* A - 2 - footer region */}
<View style={[styles.footerregion, {backgroundColor:dark?'#ffffff':'#000000'}]}>
{/* A - 2 - A - button to change theme */}
<TouchableHighlight style={styles.themebutton} underlayColor={dark?'#000000':'#ffffff'} onPress={()=>change_theme()}>
{/* A - 2 - A - 1 - theme change button text */}
<Text style={{padding:5, color:dark?'black':'white'}}>{dark?'LIGHT':'DARK'}</Text>
</TouchableHighlight>
</View>
</KeyboardAvoidingView>
);
}
//End of Main Function
/*styles (in order of heirarchy)*/
const styles = StyleSheet.create({
//full container
mainbox:{
flex:1,
},
//main body [contains login box]
bodybox:{
flex:1,
alignItems:"center",
justifyContent:'center',
},
//login box [contains login elements]
mainloginbox: {
minHeight:230,
marginHorizontal:10,
justifyContent:'space-between',
alignItems:'center',
borderRadius:4,
paddingVertical:10,
shadowOpacity:0.7,
elevation:10,
shadowRadius:8,
shadowOffset:{height:1}
},
//login heading
headerlogin:{
marginVertical:10,
padding:7,
borderRadius:4,
shadowRadius:5,
shadowOpacity:0.7,
elevation:3,
shadowOffset:{height:1}
},
//text styles for login header
headerlogintext:{
textAlign:'center',
textAlignVertical:'auto',
color:'white',
fontSize:15,
},
//userid input container main
useridbox:{
margin:10,
borderRadius:4,
flexDirection:'row',
height:40,
alignContent:'center',
justifyContent:'center',
paddingLeft:10,
shadowRadius:7,
shadowOpacity:0.7,
elevation:5,
shadowOffset:{height:0.1}
},
//input for userid
inputuserid:{
flex:1,
},
//button for userid verification
useridverify:{
marginVertical:2.5,
marginHorizontal: 5,
height:35,
width:35,
borderRadius:17.5,
shadowOpacity:0.4,
elevation:3,
shadowRadius:2,
},
//text style for login messages
resulttext:
{
color:'darkred',
marginVertical:10,
},
//password input container main
passwordbox:{
height:40,
alignContent:'center',
justifyContent:'center',
elevation:5,
margin:10,
flexDirection:'row',
borderRadius:4,
paddingLeft:10,
shadowRadius:7,
shadowOpacity:0.7,
shadowOffset:{height:1},
},
//input for password
inputpassword:{
flex:1,
},
//button for password view
passwordview:{
justifyContent:"center",
alignItems:'center',
width:45,
},
//button for credential submission
submitbutton:{
padding:8,
borderRadius:4,
alignItems:'center',
justifyContent:'center',
shadowRadius:7,
shadowOpacity:0.7,
shadowOffset:{height:1},
elevation:5
},
//text style for submit button
submitbuttontext: {
fontSize:13,
color:'white',
},
//signup container main
signupbox:{
marginVertical:10,
padding:10,
justifyContent:"center",
alignItems:'center',
borderRadius:4,
shadowRadius:10,
shadowOpacity:0.7,
shadowOffset:{height:1},
flexDirection:'row',
elevation:5,
},
//styles for signup button
signupbutton:{
},
//text style for signup button
signupbuttontext:{
fontStyle:'italic',
paddingLeft:10,
textDecorationStyle:"dashed"
},
//footer region
footerregion:{
justifyContent:'center',
height:30,
},
//theme button
themebutton:
{
alignItems:'center',
justifyContent:'center',
},
//thumbnail for buttons
buttonthumbs:{
height:35,
width:35,
},
})