This commit is contained in:
2026-03-10 17:15:26 +00:00
parent 6cdbe14875
commit c501fa7801
7 changed files with 625 additions and 574 deletions

View File

@@ -1,4 +1,4 @@
import { useRouter } from 'expo-router'; // IMPORTAR ROUTER
import { useRouter } from 'expo-router';
import { useState } from 'react';
import { ActivityIndicator, Alert, StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native';
import { supabase } from '../app/lib/supabase';
@@ -11,9 +11,8 @@ 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
const router = useRouter();
// LOGIN
const handleLogin = async () => {
if (!email || !password) {
Alert.alert('Atenção', 'Preencha todos os campos');
@@ -22,11 +21,34 @@ export default function Auth({ onLoginSuccess }: AuthProps) {
setLoading(true);
try {
const { error } = await supabase.auth.signInWithPassword({ email, password });
if (error) throw error;
// 1. LOGIN NO AUTH DO SUPABASE
const { data: { user }, error: authError } = await supabase.auth.signInWithPassword({ email, password });
if (authError) throw authError;
// 2. BUSCAR DADOS NA TABELA PROFILES LOGO APÓS O LOGIN
if (user) {
const { data: profile, error: profileError } = await supabase
.from('profiles')
.select('*')
.eq('id', user.id)
.single();
if (profileError) {
console.warn("Perfil não encontrado na tabela profiles.");
} else {
console.log("Perfil carregado com sucesso:", profile.nome);
}
}
Alert.alert('Bem-vindo(a)!');
if (onLoginSuccess) onLoginSuccess();
// 3. SE SUCESSO, EXECUTA O CALLBACK E NAVEGA
if (onLoginSuccess) {
onLoginSuccess();
} else {
router.replace('/(tabs)/estagios'); // Caminho padrão caso não venha callback
}
} catch (err: any) {
Alert.alert('Erro', err.message);
} finally {
@@ -36,7 +58,6 @@ export default function Auth({ onLoginSuccess }: AuthProps) {
return (
<View style={styles.form}>
{/* EMAIL */}
<Text style={styles.label}>Email</Text>
<TextInput
style={styles.input}
@@ -48,7 +69,6 @@ export default function Auth({ onLoginSuccess }: AuthProps) {
editable={!loading}
/>
{/* PASSWORD */}
<Text style={styles.label}>Palavra-passe</Text>
<TextInput
style={styles.input}
@@ -59,20 +79,14 @@ export default function Auth({ onLoginSuccess }: AuthProps) {
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>
)}
{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>
@@ -80,22 +94,13 @@ export default function Auth({ onLoginSuccess }: AuthProps) {
);
}
// ... (teus estilos mantêm-se iguais)
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,
},
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 },
});
});