-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
331 lines (293 loc) · 8.18 KB
/
App.tsx
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
import { SafeAreaView, ScrollView, StyleSheet, Text, TouchableOpacity, View,TextInput } from 'react-native'
import React, {useState} from 'react'
import * as Yup from 'yup'
import { Formik } from 'formik'
import BouncyCheckbox from 'react-native-bouncy-checkbox'
const PasswordSchema = Yup.object().shape({
passwordLength: Yup.number().required('Password is required')
.min(4,'Password must be at least 4 characters')
.max(20,'Password must be at most 20 characters'),
})
export default function App() {
const [password,setPassword] = useState('')
const [isPassGenerated,setIsPassGenerated] = useState(false)
const [lowerCase,setLowerCase] = useState(false)
const [upperCase,setUpperCase] = useState(false)
const [numbers,setNumbers] = useState(false)
const [symbols,setSymbols] = useState(false)
const generatePasswordString = (passwordLength:number) =>{
let characterList='';
const upperCaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
const lowerCaseChars = 'abcdefghijklmnopqrstuvwxyz'
const digiChars = '0123456789'
const specialChars = '!@#$%^&*()_+'
if (upperCase) {
characterList+=upperCaseChars
}
if(lowerCase){
characterList+=lowerCaseChars
}
if(numbers){
characterList+=digiChars
}
if(symbols){
characterList+=specialChars
}
const passwordResult= createPassword(characterList,passwordLength)
setPassword(passwordResult)
setIsPassGenerated(true)
}
const createPassword = (characters:string,passwordLength:number) =>{
let result = ''
for (let i = 0; i < passwordLength; i++) {
// const characterIndex=Math.round(Math.random()*characters.length)
const characterIndex = Math.floor(Math.random() *characters.length);
result+=characters.charAt(characterIndex)
}
return result
}
// optimized code:
// const characterIndex = Math.floor(Math.random() * charactersLength);
const resetPasswordState = () =>{
setPassword(``)
setIsPassGenerated(false)
setLowerCase(true)
setUpperCase(false)
setNumbers(false)
setSymbols(false)
}
return (
<ScrollView keyboardShouldPersistTaps="handled" style={styles.appContainer}>
<SafeAreaView>
<View style={styles.formContainer}>
<Text style={styles.title}> 🔒 PassMaster 🔒</Text>
<Text style={styles.description}>
Welcome to PassMaster! This app creates secure and complex passwords using algorithms. Users can customize length and complexity. App prioritizes security and user-friendliness. </Text>
<Formik
initialValues={{ passwordLength: ''}}
validationSchema={PasswordSchema}
onSubmit={values=>{
console.log(values);
generatePasswordString(+values.passwordLength)
}}
>
{({
values,
errors,
touched,
isValid,
handleChange,
handleSubmit,
handleReset
/* and other goodies */
}) => (
<>
<View style={styles.inputWrapper}>
<View style={styles.inputColumn}>
<Text style={styles.heading}>Password Length</Text>
{touched.passwordLength && errors.passwordLength && (
<Text style={styles.errorText}>{errors.passwordLength}</Text>
)}
</View>
<TextInput
style={styles.inputStyle}
value={values.passwordLength}
onChangeText={handleChange('passwordLength')}
placeholder="Password Length"
keyboardType='numeric'
/>
</View>
<View style={styles.inputWrapper}>
<Text style={styles.heading}>Include Lowercase Characters</Text>
<BouncyCheckbox
disableBuiltInState
size={20}
fillColor="#5DA3FA"
iconStyle={{ borderColor: "#5DA3FA" }}
onPress={() => setLowerCase(!lowerCase)}
isChecked={lowerCase}
/>
</View>
<View style={styles.inputWrapper}>
<Text style={styles.heading}>Include Uppercase Characters</Text>
<BouncyCheckbox
disableBuiltInState
size={20}
fillColor="#5DA3FA"
iconStyle={{ borderColor: "#5DA3FA" }}
onPress={() => setUpperCase(!upperCase)}
isChecked={upperCase}
/>
</View>
<View style={styles.inputWrapper}>
<Text style={styles.heading}>Include Numbers</Text>
<BouncyCheckbox
disableBuiltInState
size={20}
fillColor="#5DA3FA"
iconStyle={{ borderColor: "#5DA3FA" }}
onPress={() => setNumbers(!numbers)}
isChecked={numbers}
/>
</View>
<View style={styles.inputWrapper}>
<Text style={styles.heading}>Include Symbols</Text>
<BouncyCheckbox
disableBuiltInState
size={20}
fillColor="#5DA3FA"
iconStyle={{ borderColor: "#5DA3FA" }}
onPress={() => setSymbols(!symbols)}
isChecked={symbols}
/>
</View>
<View style={styles.inputWrapper}></View>
<View style={styles.inputWrapper}></View>
<View style={styles.inputWrapper}></View>
<View style={styles.formActions}>
<TouchableOpacity
disabled={!isValid}
style={styles.primaryBtn}
onPress={handleSubmit}
>
<Text style={styles.primaryBtnTxt}>Generate a Password</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.secondaryBtn}
onPress={()=>{
handleReset();
resetPasswordState();
}}>
<Text style={styles.secondaryBtnTxt}>Reset</Text>
</TouchableOpacity>
</View>
</>
)}
</Formik>
{isPassGenerated ? (
<View style={[styles.card,styles.cardElevated]}>
<Text style={styles.subTitle}>Here's your Password:</Text>
<Text style={styles.description}>Long Press to Copy</Text>
<Text selectable={true} style={styles.generatedPassword}>{password}</Text>
</View>
): null}
</View>
</SafeAreaView>
</ScrollView>
)
}
const styles = StyleSheet.create({
appContainer: {
flex: 1,
backgroundColor: '#031c45',
},
formContainer: {
margin: 25,
padding: 10,
},
title: {
marginTop:20,
fontSize: 32,
fontWeight: '700',
marginBottom: 15,
fontFamily: 'Roboto',
color: '#fff',
},
subTitle: {
fontSize: 20,
fontWeight: '600',
marginBottom: 6,
color: '#fff',
},
description: {
color: '#a3a3a3',
marginBottom: 25,
textAlign: 'center',
},
heading: {
fontSize: 15,
fontWeight: '800',
color: '#d1cfe3',
},
inputWrapper: {
marginBottom: 15,
alignItems: 'center',
justifyContent: 'space-between',
flexDirection: 'row',
},
inputColumn: {
flexDirection: 'column',
},
inputStyle: {
padding: 8,
width: '30%',
borderWidth: 1,
borderRadius: 4,
borderColor: '#fff',
},
errorText: {
fontSize: 12,
color: '#ff0d10',
},
formActions: {
flexDirection: 'column',
justifyContent: 'center',
marginTop: -16,
},
primaryBtn: {
width: 350,
padding: 10,
borderRadius: 8,
marginHorizontal: -3,
backgroundColor: '#1a6aed',
},
primaryBtnTxt: {
color: '#fff',
textAlign: 'center',
fontWeight: '700',
},
secondaryBtn: {
width: 350,
padding: 10,
borderRadius: 8,
marginHorizontal: -3,
marginTop: 10,
backgroundColor: '#CAD5E2',
},
secondaryBtnTxt: {
textAlign: 'center',
color: '#031c45',
fontWeight: '700',
},
card: {
padding: 12,
borderRadius: 6,
marginHorizontal: 12,
borderColor: '#fff',
width: '90%',
},
cardElevated: {
marginTop: 30,
backgroundColor: '#031c452',
marginHorizontal:-3,
// #031c45
width: '102%',
borderColor: '#fff',
borderWidth: 1,
elevation: 1,
shadowOffset: {
width: 1,
height: 1,
},
shadowColor: '#333',
shadowOpacity: 0.2,
shadowRadius: 2,
},
generatedPassword: {
fontStyle: 'italic',
fontSize: 22,
textAlign: 'center',
marginBottom: 12,
color:'#fff'
},
});