26.1.20
This commit is contained in:
101
components/Auth.tsx
Normal file
101
components/Auth.tsx
Normal file
@@ -0,0 +1,101 @@
|
||||
import { useRouter } from 'expo-router'; // IMPORTAR ROUTER
|
||||
import { useState } from 'react';
|
||||
import { ActivityIndicator, Alert, StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native';
|
||||
import { supabase } from '../app/lib/supabase';
|
||||
|
||||
interface AuthProps {
|
||||
onLoginSuccess?: () => void;
|
||||
}
|
||||
|
||||
export default function Auth({ onLoginSuccess }: AuthProps) {
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
const router = useRouter(); // INICIALIZA O ROUTER
|
||||
|
||||
// LOGIN
|
||||
const handleLogin = async () => {
|
||||
if (!email || !password) {
|
||||
Alert.alert('Atenção', 'Preencha todos os campos');
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
try {
|
||||
const { error } = await supabase.auth.signInWithPassword({ email, password });
|
||||
if (error) throw error;
|
||||
|
||||
Alert.alert('Bem-vindo(a)!');
|
||||
if (onLoginSuccess) onLoginSuccess();
|
||||
} catch (err: any) {
|
||||
Alert.alert('Erro', err.message);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<View style={styles.form}>
|
||||
{/* EMAIL */}
|
||||
<Text style={styles.label}>Email</Text>
|
||||
<TextInput
|
||||
style={styles.input}
|
||||
placeholder="email@address.com"
|
||||
value={email}
|
||||
onChangeText={setEmail}
|
||||
autoCapitalize="none"
|
||||
keyboardType="email-address"
|
||||
editable={!loading}
|
||||
/>
|
||||
|
||||
{/* PASSWORD */}
|
||||
<Text style={styles.label}>Palavra-passe</Text>
|
||||
<TextInput
|
||||
style={styles.input}
|
||||
placeholder="Insira a sua palavra-passe"
|
||||
value={password}
|
||||
onChangeText={setPassword}
|
||||
secureTextEntry
|
||||
editable={!loading}
|
||||
/>
|
||||
|
||||
{/* BOTÃO ENTRAR MODERNO */}
|
||||
<TouchableOpacity
|
||||
style={[styles.button, loading && styles.buttonDisabled]}
|
||||
onPress={handleLogin}
|
||||
disabled={loading}
|
||||
>
|
||||
{loading ? (
|
||||
<ActivityIndicator color="#fff" />
|
||||
) : (
|
||||
<Text style={styles.buttonText}>ENTRAR</Text>
|
||||
)}
|
||||
</TouchableOpacity>
|
||||
|
||||
{/* TEXTO DE ESQUECI A SENHA → NAVEGA */}
|
||||
<TouchableOpacity onPress={() => router.push('/redefenirsenha')}>
|
||||
<Text style={styles.forgotText}>Esqueceu a palavra-passe?</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
form: {
|
||||
backgroundColor: '#fff',
|
||||
borderRadius: 16,
|
||||
padding: 24,
|
||||
marginTop: 20,
|
||||
shadowColor: '#000',
|
||||
shadowOffset: { width: 0, height: 6 },
|
||||
shadowOpacity: 0.1,
|
||||
shadowRadius: 12,
|
||||
elevation: 5,
|
||||
},
|
||||
label: { fontSize: 14, fontWeight: '600', color: '#2d3436', marginBottom: 8 },
|
||||
input: { backgroundColor: '#f1f2f6', borderRadius: 12, paddingHorizontal: 16, paddingVertical: 14, fontSize: 16, marginBottom: 20, borderWidth: 0, color: '#2d3436' },
|
||||
button: { backgroundColor: '#0984e3', borderRadius: 12, paddingVertical: 16, alignItems: 'center', marginBottom: 12, shadowColor: '#0984e3', shadowOffset: { width: 0, height: 4 }, shadowOpacity: 0.3, shadowRadius: 6, elevation: 3 },
|
||||
buttonDisabled: { backgroundColor: '#74b9ff' },
|
||||
buttonText: { color: '#fff', fontSize: 17, fontWeight: '700' },
|
||||
forgotText: { color: '#0984e3', fontSize: 15, fontWeight: '500', textAlign: 'center', marginTop: 8 },
|
||||
});
|
||||
Reference in New Issue
Block a user