-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignIn.tsx
More file actions
102 lines (95 loc) · 3.17 KB
/
SignIn.tsx
File metadata and controls
102 lines (95 loc) · 3.17 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
import React, { useContext, useState } from 'react';
import {TextInput, StyleSheet, Alert, TouchableOpacity } from 'react-native';
import * as SecureStore from 'expo-secure-store';
import { ThemedView } from './ThemedView';
import { ThemedText } from './ThemedText';
import { ThemeContext } from '@/theme/ThemeContext';
const PIN_KEY = 'user_pin';
export default function SignIn({ setPassedAuth, settingPin }: { setPassedAuth: (auth: boolean) => void,settingPin?: boolean }) {
const [pin, setPin] = useState('');
const [storedPin, setStoredPin] = useState<string | null>(null);
const [isSettingPin, setIsSettingPin] = useState(settingPin || false);
const { theme } = useContext(ThemeContext)
React.useEffect(() => {
(async () => {
const savedPin = await SecureStore.getItemAsync(PIN_KEY);
if (!savedPin){
setPassedAuth(true);
}
else{setStoredPin(savedPin);}
// setIsSettingPin(!savedPin);
})();
}, []);
const handleSignIn = async () => {
if (pin === storedPin) {
setPassedAuth(true);
setPin('');
} else {
Alert.alert('Error', 'Incorrect PIN');
}
};
const handleSetPin = async () => {
if (pin.length < 4) {
Alert.alert('Error', 'PIN must be at least 4 digits');
return;
}
await SecureStore.setItemAsync(PIN_KEY, pin);
setStoredPin(pin);
setIsSettingPin(false);
setPin('');
Alert.alert('Success', 'PIN set! Please sign in.');
};
return (
<ThemedView style={[styles.container]}>
<ThemedText style={styles.title}>{isSettingPin ? 'Set a PIN' : 'Enter your PIN'}</ThemedText>
<TextInput
style={[styles.input, { color: theme === 'dark' ? '#fff' : '#000' }]}
value={pin}
onChangeText={setPin}
keyboardType="numeric"
secureTextEntry
maxLength={6}
placeholder="PIN"
placeholderTextColor={theme === 'dark' ? '#888' : '#ccc'}
/>
<TouchableOpacity
style={[
{
backgroundColor: theme === 'dark' ? '#444' : '#ddd',
paddingVertical: 14,
borderRadius: 24,
alignItems: 'center',
marginTop: 8,
}
]}
activeOpacity={0.8}
onPress={isSettingPin ? handleSetPin : handleSignIn}
>
<ThemedText style={{ color: theme === 'dark' ? '#fff' : '#000', fontSize: 18, fontWeight: 'bold' }}>
{isSettingPin ? 'Set PIN' : 'Sign In'}
</ThemedText>
</TouchableOpacity>
</ThemedView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
padding: 24,
},
title: {
fontSize: 24,
marginBottom: 24,
textAlign: 'center',
},
input: {
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 8,
padding: 12,
marginBottom: 16,
fontSize: 18,
textAlign: 'center',
},
});