1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
import { Ionicons } from "@expo/vector-icons";
import { router } from "expo-router";
import { useState } from "react";
import {
KeyboardAvoidingView,
Platform,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View,
} from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { useAuth } from "../../lib/auth";
import { useGoogleAuth } from "../../lib/google-auth";
import { t } from "../../lib/locales";
import { colors } from "../../lib/theme";
import { Button, H1, Muted, TextInput } from "../../lib/ui";
export default function SignIn() {
const { signIn } = useAuth();
const {
continueWithGoogle,
isLoading: googleLoading,
error: googleError,
} = useGoogleAuth();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [emailError, setEmailError] = useState("");
const [passwordError, setPasswordError] = useState("");
const validateForm = () => {
let valid = true;
setEmailError("");
setPasswordError("");
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
setEmailError(t("invalidEmail"));
valid = false;
}
if (!password) {
setPasswordError(t("passwordRequired"));
valid = false;
}
return valid;
};
const handleSignIn = async () => {
if (!validateForm()) return;
setLoading(true);
setError("");
try {
const result = await signIn(email, password);
if (!result.success) {
setError(result.reason || t("signInError"));
}
} catch {
setError(t("signInError"));
} finally {
setLoading(false);
}
};
return (
<SafeAreaView style={styles.container}>
<KeyboardAvoidingView
style={styles.keyboardView}
behavior={Platform.OS === "ios" ? "padding" : "height"}
>
<ScrollView
contentContainerStyle={styles.scrollContent}
keyboardShouldPersistTaps="handled"
>
<TouchableOpacity
style={styles.backButton}
onPress={() => router.back()}
>
<Ionicons name="arrow-back" size={24} color={colors.onBackground} />
</TouchableOpacity>
<View style={styles.header}>
<H1>{t("signIn")}</H1>
</View>
<View style={styles.form}>
<TextInput
label={t("email")}
value={email}
onChangeText={setEmail}
placeholder={t("emailPlaceholder")}
keyboardType="email-address"
autoCapitalize="none"
error={emailError}
/>
<TextInput
label={t("password")}
value={password}
onChangeText={setPassword}
placeholder={t("passwordPlaceholder")}
secureTextEntry
error={passwordError}
/>
<TouchableOpacity
onPress={() => router.push("/(auth)/reset-password")}
style={styles.forgotPasswordLinkContainer}
>
<Text style={styles.link}>{t("forgotPassword")}</Text>
</TouchableOpacity>
{error ? <Text style={styles.error}>{error}</Text> : null}
<Button
title={t("signIn")}
onPress={handleSignIn}
loading={loading}
disabled={loading || googleLoading}
/>
<Button
title={t("continueWithGoogle")}
variant="secondary"
onPress={continueWithGoogle}
loading={googleLoading}
disabled={loading || googleLoading}
/>
<Muted style={styles.googleHelp}>
{t("googleAuthUnifiedHint")}
</Muted>
{googleError ? (
<Text style={styles.error}>{googleError}</Text>
) : null}
</View>
<View style={styles.footer}>
<Muted>{t("dontHaveAccount")}</Muted>
<TouchableOpacity onPress={() => router.replace("/(auth)/signup")}>
<Text style={styles.link}>{t("signUp")}</Text>
</TouchableOpacity>
</View>
</ScrollView>
</KeyboardAvoidingView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: colors.background,
},
keyboardView: {
flex: 1,
},
scrollContent: {
flexGrow: 1,
padding: 24,
},
backButton: {
marginBottom: 16,
},
header: {
marginBottom: 32,
},
form: {
gap: 20,
},
error: {
color: colors.primary,
fontSize: 14,
textAlign: "center",
},
googleHelp: {
textAlign: "center",
paddingHorizontal: 8,
},
footer: {
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
gap: 8,
marginTop: 32,
},
link: {
color: colors.primary,
fontWeight: "700",
},
forgotPasswordLinkContainer: {
alignItems: "flex-end",
marginTop: -8,
},
});
|