From bbbfcc52d231c542a62659d7a4168bbf7f8693ec Mon Sep 17 00:00:00 2001 From: "nabanita.jana" Date: Fri, 29 Aug 2025 17:05:27 +0530 Subject: [PATCH] feat: update settings feature --- .../settings/change_login_password/page.tsx | 4 +- .../settings/change_txn_password/page.tsx | 3 +- .../(main)/settings/set_txn_password/page.tsx | 115 ++++++++++++------ 3 files changed, 84 insertions(+), 38 deletions(-) diff --git a/src/app/(main)/settings/change_login_password/page.tsx b/src/app/(main)/settings/change_login_password/page.tsx index 2d2c781..c09d7f2 100644 --- a/src/app/(main)/settings/change_login_password/page.tsx +++ b/src/app/(main)/settings/change_login_password/page.tsx @@ -147,11 +147,11 @@ export default function ChangePassword() { }; return ( - + + More Updates comming soon ..... Change Login Password - {/* Scrollable form area */}
+ + More Updates comming soon ..... Change Transaction Password diff --git a/src/app/(main)/settings/set_txn_password/page.tsx b/src/app/(main)/settings/set_txn_password/page.tsx index 707ee62..2be8d54 100644 --- a/src/app/(main)/settings/set_txn_password/page.tsx +++ b/src/app/(main)/settings/set_txn_password/page.tsx @@ -1,22 +1,34 @@ "use client"; 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 { IconLock } from "@tabler/icons-react"; import { generateCaptcha } from "@/app/captcha"; +import { useRouter } from "next/navigation"; export default function ChangePassword() { - const [oldPassword, setOldPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [captcha, setCaptcha] = useState(""); const [captchaInput, setCaptchaInput] = useState(""); const [otp, setOtp] = useState(""); const [otpValidated, setOtpValidated] = useState(false); + const [step, setStep] = useState<"form" | "otp" | "final">("form"); - const [step, setStep] = useState<"form" | "otp" | "final">("form"); // ✅ steps control - const [passwordHistory] = useState(["Pass@1234", "OldPass@123", "MyPass#2023"]); + const router = useRouter(); + const [passwordHistory] = useState([ + "Pass@1234", + "OldPass@123", + "MyPass#2023", + ]); const icon = ; useEffect(() => { @@ -30,10 +42,12 @@ export default function ChangePassword() { }; 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 if (step === "form") { if (!newPassword || !confirmPassword || !captchaInput) { @@ -45,8 +59,6 @@ export default function ChangePassword() { return; } - - if (!validatePasswordPolicy(newPassword)) { notifications.show({ title: "Invalid Password", @@ -85,7 +97,7 @@ export default function ChangePassword() { return; } - // ✅ Passed → move to OTP + // ✅ Passed → move to OTP step setStep("otp"); notifications.show({ title: "OTP Sent", @@ -116,19 +128,56 @@ export default function ChangePassword() { return; } - // Step 3 → Final Change Password + // Step 3 → Final API call to set transaction password if (step === "final") { - notifications.show({ - title: "Password Changed", - message: "Your password has been successfully updated.", - color: "green", - }); - resetForm(); + const token = localStorage.getItem("access_token"); + if (!token) { + router.push("/login"); + return; + } + + 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 = () => { - setOldPassword(""); setNewPassword(""); setConfirmPassword(""); setCaptchaInput(""); @@ -139,21 +188,12 @@ export default function ChangePassword() { }; return ( - + Set Transaction Password - {/* Scrollable form area */}
- {/* setOldPassword(e.currentTarget.value)} - withAsterisk - mb="xs" - /> */} e.preventDefault()} onCut={(e) => e.preventDefault()} /> - {/* CAPTCHA */} -
-