190 lines
5.5 KiB
TypeScript
190 lines
5.5 KiB
TypeScript
import React, { useState } from "react";
|
|
import {
|
|
KeyboardAvoidingView,
|
|
Platform,
|
|
ScrollView,
|
|
StyleSheet,
|
|
View,
|
|
Alert,
|
|
} from "react-native";
|
|
import { SafeAreaView } from "react-native-safe-area-context";
|
|
import { router } from "expo-router";
|
|
import { moderateScale, verticalScale } from "react-native-size-matters";
|
|
|
|
import SettingsHeader from "@/features/settings/components/SettingsHeader";
|
|
import PasswordInput from "@/components/forms/PasswordInput";
|
|
import PrimaryButton from "@/components/buttons/PrimaryButton";
|
|
import FieldLabel from "@/components/forms/FieldLabel";
|
|
import { Colors } from "@/theme";
|
|
import { apiClient } from "@/api";
|
|
|
|
export default function ChangePasswordScreen() {
|
|
const [currentPassword, setCurrentPassword] = useState("");
|
|
const [newPassword, setNewPassword] = useState("");
|
|
const [confirmPassword, setConfirmPassword] = useState("");
|
|
|
|
const [currentError, setCurrentError] = useState("");
|
|
const [newError, setNewError] = useState("");
|
|
const [confirmError, setConfirmError] = useState("");
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const handleUpdatePassword = async () => {
|
|
let hasError = false;
|
|
setCurrentError("");
|
|
setNewError("");
|
|
setConfirmError("");
|
|
|
|
if (!currentPassword) {
|
|
setCurrentError("Current password is required");
|
|
hasError = true;
|
|
}
|
|
|
|
if (!newPassword) {
|
|
setNewError("New password is required");
|
|
hasError = true;
|
|
} else if (newPassword.length < 8) {
|
|
setNewError("New password must be at least 8 characters");
|
|
hasError = true;
|
|
} else if (/\s/.test(newPassword)) {
|
|
setNewError("Password must not contain spaces");
|
|
hasError = true;
|
|
} else if (!/[A-Z]/.test(newPassword)) {
|
|
setNewError("Must contain at least one uppercase letter");
|
|
hasError = true;
|
|
} else if (!/[0-9]/.test(newPassword)) {
|
|
setNewError("Must contain at least one number");
|
|
hasError = true;
|
|
} else if (!/[^A-Za-z0-9\s]/.test(newPassword)) {
|
|
setNewError("Must contain at least one special character");
|
|
hasError = true;
|
|
}
|
|
|
|
if (!confirmPassword) {
|
|
setConfirmError("Please confirm your new password");
|
|
hasError = true;
|
|
} else if (newPassword !== confirmPassword) {
|
|
setConfirmError("Passwords do not match");
|
|
hasError = true;
|
|
}
|
|
|
|
if (hasError) return;
|
|
|
|
setIsLoading(true);
|
|
try {
|
|
await apiClient.post("/auth/change-password", {
|
|
old_password: currentPassword,
|
|
new_password: newPassword,
|
|
});
|
|
|
|
const msg = "Password updated successfully!";
|
|
if (Platform.OS === "web") {
|
|
window.alert(msg);
|
|
router.back();
|
|
} else {
|
|
Alert.alert("Success", msg, [
|
|
{ text: "OK", onPress: () => router.back() },
|
|
]);
|
|
}
|
|
} catch (err: any) {
|
|
const errMsg =
|
|
err?.response?.data?.detail ||
|
|
"Failed to update password. Please check your current password.";
|
|
setCurrentError(errMsg);
|
|
if (Platform.OS === "web") {
|
|
window.alert(errMsg);
|
|
} else {
|
|
Alert.alert("Error", errMsg);
|
|
}
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<SafeAreaView style={styles.safeArea}>
|
|
<KeyboardAvoidingView
|
|
style={styles.keyboardContainer}
|
|
behavior={Platform.OS === "ios" ? "padding" : undefined}
|
|
>
|
|
<ScrollView
|
|
keyboardShouldPersistTaps="handled"
|
|
showsVerticalScrollIndicator={false}
|
|
contentContainerStyle={styles.contentContainer}
|
|
>
|
|
<View style={styles.container}>
|
|
<SettingsHeader title="Change Password" />
|
|
|
|
<View style={styles.form}>
|
|
<View style={styles.field}>
|
|
<FieldLabel label="Current Password" required />
|
|
<PasswordInput
|
|
value={currentPassword}
|
|
onChangeText={setCurrentPassword}
|
|
placeholder="Enter current password"
|
|
error={currentError}
|
|
/>
|
|
</View>
|
|
|
|
<View style={styles.field}>
|
|
<FieldLabel label="New Password" required />
|
|
<PasswordInput
|
|
value={newPassword}
|
|
onChangeText={setNewPassword}
|
|
placeholder="Min. 8 characters with numbers & symbols"
|
|
error={newError}
|
|
/>
|
|
</View>
|
|
|
|
<View style={styles.field}>
|
|
<FieldLabel label="Confirm New Password" required />
|
|
<PasswordInput
|
|
value={confirmPassword}
|
|
onChangeText={setConfirmPassword}
|
|
placeholder="Re-enter new password"
|
|
error={confirmError}
|
|
/>
|
|
</View>
|
|
|
|
<View style={styles.buttonSpacing}>
|
|
<PrimaryButton
|
|
title="Update Password"
|
|
loading={isLoading}
|
|
onPress={handleUpdatePassword}
|
|
/>
|
|
</View>
|
|
</View>
|
|
</View>
|
|
</ScrollView>
|
|
</KeyboardAvoidingView>
|
|
</SafeAreaView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
safeArea: {
|
|
flex: 1,
|
|
backgroundColor: Colors.background,
|
|
},
|
|
keyboardContainer: {
|
|
flex: 1,
|
|
},
|
|
contentContainer: {
|
|
flexGrow: 1,
|
|
paddingBottom: verticalScale(32),
|
|
},
|
|
container: {
|
|
flex: 1,
|
|
paddingHorizontal: moderateScale(20),
|
|
paddingTop: verticalScale(12),
|
|
},
|
|
form: {
|
|
marginTop: verticalScale(8),
|
|
},
|
|
field: {
|
|
marginBottom: verticalScale(16),
|
|
},
|
|
buttonSpacing: {
|
|
marginTop: verticalScale(12),
|
|
},
|
|
});
|