26.02.19
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
// app/index.tsx
|
||||
import { useRouter } from 'expo-router';
|
||||
import {
|
||||
Alert,
|
||||
@@ -17,33 +16,42 @@ export default function LoginScreen() {
|
||||
|
||||
const handleLoginSuccess = async () => {
|
||||
try {
|
||||
// 1️⃣ buscar utilizador autenticado
|
||||
// 1️⃣ Obter utilizador autenticado
|
||||
const {
|
||||
data: { user },
|
||||
error: userError,
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
if (userError || !user) {
|
||||
console.log('Erro ao obter utilizador:', userError);
|
||||
Alert.alert('Erro', 'Utilizador não autenticado');
|
||||
return;
|
||||
}
|
||||
|
||||
// 2️⃣ buscar tipo (professor / aluno)
|
||||
console.log('Utilizador autenticado:', user.id);
|
||||
|
||||
// 2️⃣ Buscar tipo do utilizador
|
||||
const { data, error } = await supabase
|
||||
.from('profiles')
|
||||
.select('tipo')
|
||||
.eq('id', user.id)
|
||||
.single();
|
||||
|
||||
if (error || !data) {
|
||||
Alert.alert(
|
||||
'Erro',
|
||||
'Não foi possível obter o tipo de utilizador'
|
||||
);
|
||||
if (error) {
|
||||
console.log('Erro ao buscar perfil:', error);
|
||||
Alert.alert('Erro', error.message);
|
||||
return;
|
||||
}
|
||||
|
||||
// 3️⃣ redirecionar conforme o tipo
|
||||
if (!data) {
|
||||
console.log('Perfil não encontrado');
|
||||
Alert.alert('Erro', 'Perfil não encontrado');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Tipo de utilizador:', data.tipo);
|
||||
|
||||
// 3️⃣ Redirecionar conforme o tipo
|
||||
if (data.tipo === 'professor') {
|
||||
router.replace('/Professor/ProfessorHome');
|
||||
} else if (data.tipo === 'aluno') {
|
||||
@@ -52,6 +60,7 @@ export default function LoginScreen() {
|
||||
Alert.alert('Erro', 'Tipo de utilizador inválido');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Erro inesperado:', err);
|
||||
Alert.alert('Erro', 'Erro inesperado no login');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user