eletrotupi / tcc/ frontend/app/auth/login.tsxffd33a29c8ba15703f785399b5d0352f50c16d73
4.4 KB | 173 lines Raw
1
import React, { useState } from 'react';
2
import {
3
  View,
4
  Text,
5
  StyleSheet,
6
  ScrollView,
7
  KeyboardAvoidingView,
8
  Platform,
9
  Alert,
10
} from 'react-native';
11
import { Link, router } from 'expo-router';
12
import { SafeAreaView } from 'react-native-safe-area-context';
13
import { useThemeColor } from '@/hooks/use-theme-color';
14
import { Button, Input } from '@/components/ui';
15
import { useAuth } from '@/context/AuthContext';
16
import { apiClient, User } from '@/lib/api';
17

18
export default function LoginScreen() {
19
  const [email, setEmail] = useState('');
20
  const [password, setPassword] = useState('');
21
  const [errors, setErrors] = useState<{ email?: string; password?: string }>({});
22

23
  const { login, isLoading } = useAuth();
24
  const textColor = useThemeColor({}, 'text');
25
  const backgroundColor = useThemeColor({}, 'background');
26
  const tintColor = useThemeColor({}, 'tint');
27

28
  const validateForm = () => {
29
    const newErrors: { email?: string; password?: string } = {};
30

31
    if (!email) {
32
      newErrors.email = 'Email é obrigatório';
33
    } else if (!/\S+@\S+\.\S+/.test(email)) {
34
      newErrors.email = 'Por favor, use um email válido';
35
    }
36

37
    if (!password) {
38
      newErrors.password = 'Senha é obrigatória';
39
    }
40

41
    setErrors(newErrors);
42
    return Object.keys(newErrors).length === 0;
43
  };
44

45
  const handleLogin = async () => {
46
    if (!validateForm()) return;
47

48
    try {
49
      await login(email, password);
50
      router.replace('/(tabs)/new');
51
    } catch (error: any) {
52
	  Alert.alert('Erro: ', 'Falha ao fazer login');
53
    }
54
  };
55

56
  return (
57
    <SafeAreaView style={[styles.container, { backgroundColor }]}>
58
      <KeyboardAvoidingView
59
        behavior='height'
60
        style={styles.keyboardView}
61
      >
62
        <ScrollView
63
          contentContainerStyle={styles.scrollContainer}
64
          showsVerticalScrollIndicator={false}
65
        >
66
          <View style={styles.header}>
67
            <Text style={[styles.title, { color: textColor }]}>Bem vindo de volta</Text>
68
            <Text style={[styles.subtitle, { color: textColor, opacity: 0.7 }]}>
69
              Logue na sua conta
70
            </Text>
71
          </View>
72

73
          <View style={styles.form}>
74
            <Input
75
              label="Email"
76
              type="email"
77
              value={email}
78
              onChangeText={setEmail}
79
              placeholder="Qual seu email?"
80
              error={errors.email}
81
              autoCapitalize="none"
82
            />
83

84
            <Input
85
              label="Senha"
86
              type="password"
87
              value={password}
88
              onChangeText={setPassword}
89
              placeholder="Qual sua senha?"
90
              error={errors.password}
91
              showPasswordToggle
92
            />
93

94
            <Button
95
              title="Entrar"
96
              onPress={handleLogin}
97
              loading={isLoading}
98
              style={styles.loginButton}
99
            />
100

101
            <Link href="/auth/forgot-password" style={[styles.link, {color: tintColor }]} asChild>
102
              <Text style={[styles.footerText, { color: textColor, opacity: 0.7 }]}>
103
                Esqueceu a senha?
104
              </Text>
105
            </Link>
106
          </View>
107

108
          <View style={styles.footer}>
109
            <Text style={[styles.footerText, { color: textColor, opacity: 0.7 }]}>
110
              Não tem uma conta ainda?{' '}
111
            </Text>
112

113
            <Link href="/auth/signup" asChild style={[styles.link, { color: tintColor }]}>
114
              <Text style={[styles.footerText, { color: textColor, opacity: 0.7 }]}>
115
                Cadastre-se
116
              </Text>
117
            </Link>
118
          </View>
119
        </ScrollView>
120
      </KeyboardAvoidingView>
121
    </SafeAreaView>
122
  );
123
}
124

125
const styles = StyleSheet.create({
126
  container: {
127
    flex: 1,
128
  },
129
  keyboardView: {
130
    flex: 1,
131
  },
132
  scrollContainer: {
133
    flexGrow: 1,
134
    paddingHorizontal: 24,
135
    justifyContent: 'center',
136
    minHeight: '100%',
137
  },
138
  header: {
139
    alignItems: 'center',
140
    marginBottom: 40,
141
  },
142
  title: {
143
    fontSize: 28,
144
    fontWeight: 'bold',
145
    marginBottom: 8,
146
  },
147
  subtitle: {
148
    fontSize: 16,
149
    textAlign: 'center',
150
  },
151
  form: {
152
    marginBottom: 32,
153
  },
154
  loginButton: {
155
    marginTop: 8,
156
    marginBottom: 16,
157
  },
158
  link: {
159
    textAlign: 'center',
160
    fontSize: 14,
161
    fontWeight: '500',
162
  },
163
  footer: {
164
    flexDirection: 'row',
165
    justifyContent: 'center',
166
    alignItems: 'center',
167
    marginTop: 'auto',
168
    paddingBottom: 20,
169
  },
170
  footerText: {
171
    fontSize: 14,
172
  },
173
});