142 lines
3.1 KiB
TypeScript
142 lines
3.1 KiB
TypeScript
import React, { useState } from 'react';
|
|
import {
|
|
View,
|
|
Text,
|
|
TextInput,
|
|
TouchableOpacity,
|
|
StyleSheet,
|
|
Alert,
|
|
KeyboardAvoidingView,
|
|
Platform,
|
|
ActivityIndicator,
|
|
} from 'react-native';
|
|
import { useAuth } from '../contexts/AuthContext';
|
|
|
|
export function LoginScreen({ navigation }: any) {
|
|
const { login } = useAuth();
|
|
const [username, setUsername] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
async function handleLogin() {
|
|
if (!username.trim() || !password) {
|
|
Alert.alert('Erreur', 'Veuillez remplir tous les champs');
|
|
return;
|
|
}
|
|
|
|
setLoading(true);
|
|
try {
|
|
await login(username.trim(), password);
|
|
} catch (error: any) {
|
|
Alert.alert('Erreur', error.message || 'Connexion échouée');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<KeyboardAvoidingView
|
|
style={styles.container}
|
|
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
|
|
>
|
|
<View style={styles.inner}>
|
|
<Text style={styles.title}>Dot.</Text>
|
|
<Text style={styles.subtitle}>Connectez-vous à votre compte</Text>
|
|
|
|
<TextInput
|
|
style={styles.input}
|
|
placeholder="Nom d'utilisateur"
|
|
value={username}
|
|
onChangeText={setUsername}
|
|
autoCapitalize="none"
|
|
autoCorrect={false}
|
|
editable={!loading}
|
|
/>
|
|
|
|
<TextInput
|
|
style={styles.input}
|
|
placeholder="Mot de passe"
|
|
value={password}
|
|
onChangeText={setPassword}
|
|
secureTextEntry
|
|
editable={!loading}
|
|
/>
|
|
|
|
<TouchableOpacity
|
|
style={[styles.button, loading && styles.buttonDisabled]}
|
|
onPress={handleLogin}
|
|
disabled={loading}
|
|
>
|
|
{loading ? (
|
|
<ActivityIndicator color="#fff" />
|
|
) : (
|
|
<Text style={styles.buttonText}>Se connecter</Text>
|
|
)}
|
|
</TouchableOpacity>
|
|
|
|
<TouchableOpacity
|
|
style={styles.linkButton}
|
|
onPress={() => navigation.navigate('Register')}
|
|
disabled={loading}
|
|
>
|
|
<Text style={styles.linkText}>Pas de compte ? S'inscrire</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
</KeyboardAvoidingView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
flex: 1,
|
|
backgroundColor: '#fff',
|
|
},
|
|
inner: {
|
|
flex: 1,
|
|
justifyContent: 'center',
|
|
paddingHorizontal: 32,
|
|
},
|
|
title: {
|
|
fontSize: 32,
|
|
fontWeight: 'bold',
|
|
textAlign: 'center',
|
|
marginBottom: 8,
|
|
},
|
|
subtitle: {
|
|
fontSize: 16,
|
|
color: '#666',
|
|
textAlign: 'center',
|
|
marginBottom: 32,
|
|
},
|
|
input: {
|
|
borderWidth: 1,
|
|
borderColor: '#ddd',
|
|
borderRadius: 8,
|
|
padding: 16,
|
|
fontSize: 16,
|
|
marginBottom: 16,
|
|
},
|
|
button: {
|
|
backgroundColor: '#000',
|
|
borderRadius: 8,
|
|
padding: 16,
|
|
alignItems: 'center',
|
|
marginBottom: 16,
|
|
},
|
|
buttonDisabled: {
|
|
opacity: 0.6,
|
|
},
|
|
buttonText: {
|
|
color: '#fff',
|
|
fontSize: 16,
|
|
fontWeight: '600',
|
|
},
|
|
linkButton: {
|
|
alignItems: 'center',
|
|
},
|
|
linkText: {
|
|
color: '#000',
|
|
fontSize: 14,
|
|
},
|
|
});
|