feat: update settings feature

This commit is contained in:
2025-08-29 17:05:27 +05:30
parent 3444291e25
commit bbbfcc52d2
3 changed files with 84 additions and 38 deletions

View File

@@ -147,11 +147,11 @@ export default function ChangePassword() {
}; };
return ( return (
<Paper shadow="sm" radius="md" p="md" withBorder> <Paper shadow="sm" radius="md" p="md" withBorder h={400}>
<Title order={3} style={{color:"red"}}>More Updates comming soon .....</Title>
<Title order={3} mb="sm"> <Title order={3} mb="sm">
Change Login Password Change Login Password
</Title> </Title>
{/* Scrollable form area */} {/* Scrollable form area */}
<div style={{ overflowY: "auto", maxHeight: "280px", paddingRight: 8 }}> <div style={{ overflowY: "auto", maxHeight: "280px", paddingRight: 8 }}>
<PasswordInput <PasswordInput

View File

@@ -147,7 +147,8 @@ export default function ChangePassword() {
}; };
return ( return (
<Paper shadow="sm" radius="md" p="md" withBorder> <Paper shadow="sm" radius="md" p="md" withBorder h={400}>
<Title order={3} style={{color:"red"}}>More Updates comming soon .....</Title>
<Title order={3} mb="sm"> <Title order={3} mb="sm">
Change Transaction Password Change Transaction Password
</Title> </Title>

View File

@@ -1,22 +1,34 @@
"use client"; "use client";
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { TextInput, PasswordInput, Button, Title, Paper, Group, Box } from "@mantine/core"; import {
TextInput,
PasswordInput,
Button,
Title,
Paper,
Group,
} from "@mantine/core";
import { notifications } from "@mantine/notifications"; import { notifications } from "@mantine/notifications";
import { IconLock } from "@tabler/icons-react"; import { IconLock } from "@tabler/icons-react";
import { generateCaptcha } from "@/app/captcha"; import { generateCaptcha } from "@/app/captcha";
import { useRouter } from "next/navigation";
export default function ChangePassword() { export default function ChangePassword() {
const [oldPassword, setOldPassword] = useState("");
const [newPassword, setNewPassword] = useState(""); const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState("");
const [captcha, setCaptcha] = useState(""); const [captcha, setCaptcha] = useState("");
const [captchaInput, setCaptchaInput] = useState(""); const [captchaInput, setCaptchaInput] = useState("");
const [otp, setOtp] = useState(""); const [otp, setOtp] = useState("");
const [otpValidated, setOtpValidated] = useState(false); const [otpValidated, setOtpValidated] = useState(false);
const [step, setStep] = useState<"form" | "otp" | "final">("form");
const [step, setStep] = useState<"form" | "otp" | "final">("form"); // ✅ steps control const router = useRouter();
const [passwordHistory] = useState(["Pass@1234", "OldPass@123", "MyPass#2023"]); const [passwordHistory] = useState([
"Pass@1234",
"OldPass@123",
"MyPass#2023",
]);
const icon = <IconLock size={18} stroke={1.5} />; const icon = <IconLock size={18} stroke={1.5} />;
useEffect(() => { useEffect(() => {
@@ -30,10 +42,12 @@ export default function ChangePassword() {
}; };
const validatePasswordPolicy = (password: string) => { const validatePasswordPolicy = (password: string) => {
return /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,}$/.test(password); return /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,}$/.test(
password
);
}; };
const handleSubmit = () => { const handleSubmit = async () => {
// Step 1 → validate form // Step 1 → validate form
if (step === "form") { if (step === "form") {
if (!newPassword || !confirmPassword || !captchaInput) { if (!newPassword || !confirmPassword || !captchaInput) {
@@ -45,8 +59,6 @@ export default function ChangePassword() {
return; return;
} }
if (!validatePasswordPolicy(newPassword)) { if (!validatePasswordPolicy(newPassword)) {
notifications.show({ notifications.show({
title: "Invalid Password", title: "Invalid Password",
@@ -85,7 +97,7 @@ export default function ChangePassword() {
return; return;
} }
// ✅ Passed → move to OTP // ✅ Passed → move to OTP step
setStep("otp"); setStep("otp");
notifications.show({ notifications.show({
title: "OTP Sent", title: "OTP Sent",
@@ -116,19 +128,56 @@ export default function ChangePassword() {
return; return;
} }
// Step 3 → Final Change Password // Step 3 → Final API call to set transaction password
if (step === "final") { if (step === "final") {
notifications.show({ const token = localStorage.getItem("access_token");
title: "Password Changed", if (!token) {
message: "Your password has been successfully updated.", router.push("/login");
color: "green", return;
}); }
resetForm();
try {
const response = await fetch("/api/auth/transaction_password", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
transaction_password: newPassword,
}),
});
if (response.status === 401) {
localStorage.removeItem("access_token");
router.push("/login");
return;
}
const result = await response.json();
if (!response.ok) {
throw new Error(result.message || "Failed to set transaction password");
}
notifications.show({
title: "Success",
message: "Transaction password set successfully.",
color: "green",
});
resetForm();
} catch (err: any) {
notifications.show({
title: "Error",
message: err.message || "Server error, please try again later",
color: "red",
});
}
} }
}; };
const resetForm = () => { const resetForm = () => {
setOldPassword("");
setNewPassword(""); setNewPassword("");
setConfirmPassword(""); setConfirmPassword("");
setCaptchaInput(""); setCaptchaInput("");
@@ -139,21 +188,12 @@ export default function ChangePassword() {
}; };
return ( return (
<Paper shadow="sm" radius="md" p="md" withBorder> <Paper shadow="sm" radius="md" p="md" withBorder h={400} >
<Title order={3} mb="sm"> <Title order={3} mb="sm">
Set Transaction Password Set Transaction Password
</Title> </Title>
{/* Scrollable form area */}
<div style={{ overflowY: "auto", maxHeight: "280px", paddingRight: 8 }}> <div style={{ overflowY: "auto", maxHeight: "280px", paddingRight: 8 }}>
{/* <PasswordInput
label="Old Password"
placeholder="Enter old password"
value={oldPassword}
onChange={(e) => setOldPassword(e.currentTarget.value)}
withAsterisk
mb="xs"
/> */}
<Group grow> <Group grow>
<PasswordInput <PasswordInput
label="New Password" label="New Password"
@@ -177,17 +217,23 @@ export default function ChangePassword() {
onPaste={(e) => e.preventDefault()} onPaste={(e) => e.preventDefault()}
onCut={(e) => e.preventDefault()} onCut={(e) => e.preventDefault()}
/> />
</Group> </Group>
{/* CAPTCHA */} {/* CAPTCHA */}
<div style={{ marginTop: 5 }}> <div style={{ marginTop: 5 }}>
<label style={{ display: "block", marginBottom: 4, fontSize: "14px" }}> <label
style={{ display: "block", marginBottom: 4, fontSize: "14px" }}
>
Enter CAPTCHA <span style={{ color: "red" }}>*</span> Enter CAPTCHA <span style={{ color: "red" }}>*</span>
</label> </label>
<div style={{ display: "flex", alignItems: "center", gap: 10, marginBottom: 5 }}> <div
style={{
display: "flex",
alignItems: "center",
gap: 10,
marginBottom: 5,
}}
>
<div <div
style={{ style={{
fontSize: "18px", fontSize: "18px",
@@ -224,6 +270,7 @@ export default function ChangePassword() {
/> />
</div> </div>
</div> </div>
{step !== "form" && ( {step !== "form" && (
<PasswordInput <PasswordInput
label="Enter OTP" label="Enter OTP"
@@ -236,15 +283,13 @@ export default function ChangePassword() {
readOnly={otpValidated} readOnly={otpValidated}
/> />
)} )}
</div> </div>
{/* Buttons */}
<Group mt="md" gap="sm"> <Group mt="md" gap="sm">
<Button onClick={handleSubmit}> <Button onClick={handleSubmit}>
{step === "form" && "Submit"} {step === "form" && "Submit"}
{step === "otp" && "Validate OTP"} {step === "otp" && "Validate OTP"}
{step === "final" && "Change Password"} {step === "final" && "Set Password"}
</Button> </Button>
<Button variant="outline" color="gray" onClick={resetForm}> <Button variant="outline" color="gray" onClick={resetForm}>
Reset Reset