acupedia-mobile-ui/src/features/auth/components/SignUpForm.tsx

189 lines
5.4 KiB
TypeScript

import React, { useState } from "react";
import { Alert, StyleSheet, View } from "react-native";
import { useRouter } from "expo-router";
import { verticalScale } from "react-native-size-matters";
import BaseTextInput from "@/components/forms/BaseTextInput";
import PasswordInput from "@/components/forms/PasswordInput";
import PrimaryButton from "@/components/buttons/PrimaryButton";
import { useAuthStore } from "@/store/auth.store";
import { register } from "@/features/auth/api/auth.api";
export default function SignUpForm() {
const router = useRouter();
const setRegisteredUser = useAuthStore((state) => state.setRegisteredUser);
const [fullName, setFullName] = useState("");
const [email, setEmail] = useState("");
const [phone, setPhone] = useState("");
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [fullNameError, setFullNameError] = useState("");
const [emailError, setEmailError] = useState("");
const [phoneError, setPhoneError] = useState("");
const [passwordError, setPasswordError] = useState("");
const [confirmPasswordError, setConfirmPasswordError] = useState("");
const handlePhoneChange = (text: string) => {
const digits = text.replace(/\D/g, "");
setPhone(digits.slice(0, 10));
};
const handleSignUp = async () => {
let hasError = false;
setFullNameError("");
setEmailError("");
setPhoneError("");
setPasswordError("");
setConfirmPasswordError("");
if (!fullName.trim()) {
setFullNameError("Full name is required");
hasError = true;
}
if (!email.trim()) {
setEmailError("Email is required");
hasError = true;
} else if (!/\S+@\S+\.\S+/.test(email)) {
setEmailError("Please enter a valid email address");
hasError = true;
}
if (phone.trim() && phone.length < 10) {
setPhoneError("Phone number must be 10 digits");
hasError = true;
}
if (!password) {
setPasswordError("Password is required");
hasError = true;
} else if (password.length < 8) {
setPasswordError("Password must be at least 8 characters");
hasError = true;
} else if (/\s/.test(password)) {
setPasswordError("Password must not contain spaces");
hasError = true;
} else if (!/[A-Z]/.test(password)) {
setPasswordError("Password must contain at least one uppercase letter (A-Z)");
hasError = true;
} else if (!/[0-9]/.test(password)) {
setPasswordError("Password must contain at least one number (0-9)");
hasError = true;
} else if (!/[^A-Za-z0-9\s]/.test(password)) {
setPasswordError("Password must contain at least one special character (!@#$)");
hasError = true;
}
if (!confirmPassword) {
setConfirmPasswordError("Please confirm your password");
hasError = true;
} else if (password !== confirmPassword) {
setConfirmPasswordError("Passwords do not match");
hasError = true;
}
if (hasError) return;
try {
const response = await register({
email: email.trim().toLowerCase(),
password,
full_name: fullName.trim(),
phone_number: phone.trim() ? phone.trim() : undefined,
});
const { user } = response;
setRegisteredUser({
id: user.id,
name: user.display_name,
email: user.email,
});
// Redirect directly to login screen without alert block
router.replace("/login");
} catch (err: any) {
let errMsg = "Something went wrong. Please try again.";
const detail = err.response?.data?.detail;
if (typeof detail === "string") {
errMsg = detail;
} else if (Array.isArray(detail) && detail.length > 0) {
errMsg = detail[0].msg || JSON.stringify(detail[0]);
}
alert("Registration Failed: " + errMsg);
}
};
return (
<View style={styles.container}>
<BaseTextInput
value={fullName}
onChangeText={setFullName}
placeholder="Full Name"
autoCapitalize="words"
autoCorrect={false}
leftIcon="person-outline"
error={fullNameError}
/>
<BaseTextInput
value={email}
onChangeText={setEmail}
placeholder="Email Address"
keyboardType="email-address"
autoCapitalize="none"
autoCorrect={false}
textContentType="emailAddress"
leftIcon="mail-outline"
error={emailError}
/>
<BaseTextInput
value={phone}
onChangeText={handlePhoneChange}
placeholder="Phone Number (Optional)"
keyboardType="number-pad"
autoCapitalize="none"
autoCorrect={false}
textContentType="telephoneNumber"
maxLength={10}
leftIcon="call-outline"
error={phoneError}
/>
<PasswordInput
value={password}
onChangeText={setPassword}
placeholder="Password"
error={passwordError}
/>
<PasswordInput
value={confirmPassword}
onChangeText={setConfirmPassword}
placeholder="Confirm Password"
error={confirmPasswordError}
/>
<View style={styles.buttonSpacing}>
<PrimaryButton
title="Sign Up"
onPress={handleSignUp}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
marginTop: verticalScale(10),
},
buttonSpacing: {
marginTop: verticalScale(10),
},
});