summaryrefslogtreecommitdiff
path: root/app/(auth)/signup.tsx
blob: 127ea9f835b2bbf2697cac06b2c8db3149d99d6a (plain)
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
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 { t } from "../../lib/locales";
import { colors } from "../../lib/theme";
import { Button, H1, Muted, TextInput } from "../../lib/ui";

export default function SignUp() {
  const { signUp } = useAuth();
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [confirmPassword, setConfirmPassword] = useState("");
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
  const [emailError, setEmailError] = useState("");
  const [passwordError, setPasswordError] = useState("");
  const [confirmPasswordError, setConfirmPasswordError] = useState("");

  const validateForm = () => {
    let valid = true;
    setEmailError("");
    setPasswordError("");
    setConfirmPasswordError("");

    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(email)) {
      setEmailError(t("invalidEmail"));
      valid = false;
    }

    if (!password) {
      setPasswordError(t("passwordRequired"));
      valid = false;
    } else if (password.length < 8) {
      setPasswordError(t("passwordTooShort"));
      valid = false;
    }

    if (password !== confirmPassword) {
      setConfirmPasswordError(t("passwordsDoNotMatch"));
      valid = false;
    }

    return valid;
  };

  const handleSignUp = async () => {
    if (!validateForm()) return;

    setLoading(true);
    setError("");

    try {
      const result = await signUp(email, password);
      if (!result.success) {
        setError(result.reason || t("signUpError"));
      }
    } catch (e) {
      setError(t("signUpError"));
    } 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("createAccount")}</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}
            />

            <TextInput
              label={t("confirmPassword")}
              value={confirmPassword}
              onChangeText={setConfirmPassword}
              placeholder={t("confirmPasswordPlaceholder")}
              secureTextEntry
              error={confirmPasswordError}
            />

            {error ? <Text style={styles.error}>{error}</Text> : null}

            <Button
              title={t("signUp")}
              onPress={handleSignUp}
              loading={loading}
              disabled={loading}
            />
          </View>

          <View style={styles.footer}>
            <Muted>{t("alreadyHaveAccount")}</Muted>
            <TouchableOpacity onPress={() => router.replace("/(auth)/signin")}>
              <Text style={styles.link}>{t("signIn")}</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",
  },
  footer: {
    flexDirection: "row",
    justifyContent: "center",
    alignItems: "center",
    gap: 8,
    marginTop: 32,
  },
  link: {
    color: colors.primary,
    fontWeight: "700",
  },
});