"use client"; import React, { useState, useEffect, memo, useRef } from "react"; import { Text, Button, TextInput, PasswordInput, Title, Card, Group, Flex, Box, Image, Anchor, Tooltip, Modal } from "@mantine/core"; import { notifications } from "@mantine/notifications"; import { Providers } from "@/app/providers"; import { useRouter } from "next/navigation"; import { sendOtp, verifyLoginOtp } from '@/app/_util/otp'; import NextImage from "next/image"; import styles from './page.module.css'; import logo from '@/app/image/logo1.jpg'; import frontPage from '@/app/image/ib_front_1.jpg'; import dynamic from 'next/dynamic'; import { generateCaptcha } from '@/app/captcha'; import { IconRefresh, IconShieldLockFilled } from "@tabler/icons-react"; import dayjs from "dayjs"; import { fetchAndStoreUserName } from "../_util/userdetails"; export default function Login() { const router = useRouter(); const [CIF, SetCIF] = useState(""); const [psw, SetPsw] = useState(""); const [captcha, setCaptcha] = useState(""); const [inputCaptcha, setInputCaptcha] = useState(""); const [isLogging, setIsLogging] = useState(false); const ClientCarousel = dynamic(() => import('./clientCarousel'), { ssr: false }); const headerRef = useRef(null); const [opened, setOpened] = useState(false); const [otpStep, setOtpStep] = useState(false); const [otp, setOtp] = useState(""); const [loading, setLoading] = useState(false); const [otpRequired, setOtpRequired] = useState(false); const [otpVerified, setOtpVerified] = useState(false); const [buttonLabel, setButtonLabel] = useState("Submit"); const [mobile, setMobile] = useState(""); async function handleSendOtp(mobile?: string) { if (!mobile) { notifications.show({ title: 'Error', message: 'Mobile number not found. Contact administrator.', color: 'red', }); return false; } try { // await sendOtp({ type: 'LOGIN_OTP', username: CIF, mobileNumber: mobile }); await sendOtp({ type: 'LOGIN_OTP', username: CIF, mobileNumber: "6297421727" }); notifications.show({ color: 'orange', title: 'OTP Required', message: 'OTP sent to your registered mobile number.', }); return true; } catch (err: any) { notifications.show({ title: 'Error', message: err?.message || 'Send OTP failed. Please try again later.', color: 'red', }); return false; } } async function handleVerifyOtp(mobile?: string) { try { if (mobile) { // await verifyLoginOtp(otp, mobile); await verifyLoginOtp(otp, '6297421727'); return true; } } catch (err: any) { notifications.show({ title: `${err.message}`, message: 'OTP verification failed. Please try again later.', color: 'red', }); return false; } } useEffect(() => { const loadCaptcha = async () => { const newCaptcha = await generateCaptcha(); setCaptcha(newCaptcha); }; loadCaptcha(); }, []); useEffect(() => { const headerData = [ "THE KANGRA CENTRAL CO-OPERATIVE BANK LTD.", "कांगड़ा केन्द्रीय सहकारी बैंक सीमित", ]; let index = 0; const interval = setInterval(() => { index = (index + 1) % headerData.length; if (headerRef.current) { headerRef.current.textContent = headerData[index]; } }, 2000); return () => clearInterval(interval); }, []); const regenerateCaptcha = () => { const loadCaptcha = async () => { const newCaptcha = await generateCaptcha(); setCaptcha(newCaptcha); }; loadCaptcha(); setInputCaptcha(""); }; async function handleLogin(e: React.FormEvent) { e.preventDefault(); if (isLogging) return; setIsLogging(true); // show loading & disable inputs try { // --- Validation (before any API call) --- if (!otpRequired && !otpVerified) { const onlyDigit = /^\d{11}$/; if (!onlyDigit.test(CIF)) { notifications.show({ withBorder: true, color: "red", title: "Invalid UserId", message: "UserID must be 11 digit", autoClose: 5000, }); setIsLogging(false); return; } if (!inputCaptcha) { notifications.show({ withBorder: true, color: "red", title: "Invalid Captcha", message: "Please fill the Captcha field", autoClose: 5000, }); setIsLogging(false); return; } if (inputCaptcha !== captcha) { notifications.show({ withBorder: true, color: "red", title: "Captcha Error", message: "Please enter the correct captcha", autoClose: 5000, }); regenerateCaptcha(); setIsLogging(false); return; } if (!CIF || !psw) { notifications.show({ withBorder: true, color: "red", title: "Invalid Input", message: "Please fill UserId and Password", autoClose: 5000, }); setIsLogging(false); return; } } // --- LOGIN API FLOW --- if (!otpRequired || otpVerified) { const response = await fetch("api/auth/login", { method: "POST", headers: { "Content-Type": "application/json", "X-Login-Type": "IB", }, body: JSON.stringify({ customerNo: CIF, password: psw, otp: otp, }), }); const data = await response.json(); // console.log(data); // 1️⃣ OTP Required if (data.status === "OTP_REQUIRED" && response.status === 202) { setMobile(data.mobile); setOtpRequired(true); setButtonLabel("Verify OTP"); const otpSent = await handleSendOtp(data.mobile); setIsLogging(false); return; } // 2️⃣ Migrated user (no password) if (data.error === "MIGRATED_USER_HAS_NO_PASSWORD") { setOpened(true); setIsLogging(false); return; } // 3️⃣ Invalid login if (!response.ok) { notifications.show({ withBorder: true, color: "red", title: "Error", message: data?.error || "Internal Server Error", autoClose: 5000, }); regenerateCaptcha(); localStorage.removeItem("access_token"); localStorage.clear(); sessionStorage.clear(); setIsLogging(false); return; } // 4️⃣ Successful login setOtp(""); const token = data.token; localStorage.setItem("access_token", token); localStorage.setItem("pswExpiryDate", data.loginPswExpiry); if (data.loginPswExpiry && dayjs(data.loginPswExpiry).diff(dayjs(), "day") < 0) { notifications.show({ withBorder: true, color: "orange", title: "Password Expired", message: "Your password has expired, please set a new one.", autoClose: 4000, }); router.push("/ChangePassword"); return; } // Fetching mobile no and user name await fetchAndStoreUserName(token); if (data.FirstTimeLogin === true) { router.push("/SetPassword"); } else { router.push("/home"); } } // --- OTP Verification Flow --- if (otpRequired && !otpVerified) { if (!otp) { notifications.show({ color: "red", title: "Invalid OTP", message: "Please enter OTP before verifying", }); setIsLogging(false); return; } const verified = await handleVerifyOtp(mobile); if (!verified) { setIsLogging(false); return; } notifications.show({ color: "Blue", title: "OTP Verified", message: "Please click Login to continue", }); setOtpVerified(true); setButtonLabel("Login"); setIsLogging(false); return; } } catch (error: any) { notifications.show({ withBorder: true, color: "red", title: "Error", message: "Internal Server Error, Please try again later", autoClose: 5000, }); } finally { // Ensure we always stop loader if still active setIsLogging(false); } } const sendMigrationOtp = async () => { try { setLoading(true); const res = await fetch(`/api/otp/send/set-password?customerNo=${CIF}`, { method: "GET", headers: { "Content-Type": "application/json" }, }); if (!res.ok) throw new Error("Failed to send OTP"); notifications.show({ color: "green", title: "OTP Sent", message: "Please check your registered mobile.", }); setOtpStep(true); } catch (err: any) { notifications.show({ color: "red", title: "Error", message: err.message || "Could not send OTP", }); } finally { setLoading(false); } }; // For migration User const verifyMigrationOtp = async () => { if (!otp) { notifications.show({ color: "red", title: "Invalid Input", message: "Please enter OTP before verifying", }); return; } try { setLoading(true); const res = await fetch(`/api/otp/verify/set-password?customerNo=${CIF}&otp=${otp}`, { method: "GET", headers: { "Content-Type": "application/json" }, }); const data = await res.json(); // console.log(data) if (!res.ok) { notifications.show({ color: "red", title: "Error", message: data.error || "OTP verification failed", }); return; } localStorage.setItem("access_token", data.token); notifications.show({ color: "green", title: "OTP Verified", message: "Redirecting to set password page...", }); setOpened(false); router.push("/SetPassword"); } catch (err: any) { notifications.show({ color: "red", title: "Error", message: err.message || "OTP verification failed", }); } finally { setLoading(false); } }; return ( { setOpened(false); setOtpStep(false); setOtp(""); }} title="User Migration Notice" centered > {!otpStep ? ( // STEP 1: Migration Notice <> Your account is being migrated to the new system. You need to set a new password to continue login. {/* Call the API for send otp */} ) : ( // STEP 2: OTP Verification (Frontend-only) <> Enter the OTP sent to your registered mobile setOtp(e.currentTarget.value)} /> )} {/* Main Screen */}
{/* Header */} ebanking {/* Desktop */} THE KANGRA CENTRAL CO-OPERATIVE BANK LTD. Head Office: Dharmshala, District Kangra (H.P), Pin: 176215 {/* Mobile */} THE KANGRA CENTRAL CO-OPERATIVE BANK LTD. Head Office: Dharmshala, District Kangra (H.P), Pin: 176215
{/* Movable text */} ⚠️ Always login to our Net Banking site directly or through Banks website. ⚠️ Do not disclose your User Id and Password to any third party and keep Your User Id and Password strictly confidential. ⚠️ KCC Bank never asks for User Id,Passwords and Pins through email or phone. ⚠️ Be ware of Phishing mails with links to fake bank's websites asking for personal information are in circulation. ⚠️ Please DO NOT Click on the links given in the emails asking for personal details like bank account number, user ID and password. ⚠️ If you had shared your User Id and Password through such mails or links, please change your Password immediately. ⚠️ Inform the Bank/branch in which your account is maintained for resetting your password. {/* Main */}
ebanking
{ const input = e.currentTarget.value.replace(/\D/g, ""); if (input.length <= 11) SetCIF(input); }} withAsterisk // disabled={otpRequired} disabled={isLogging || otpRequired} readOnly={isLogging} /> SetPsw(e.currentTarget.value)} withAsterisk // disabled={otpRequired} disabled={isLogging || otpRequired} readOnly={isLogging} // mt="sm" /> {/* router.push("/ValidateUser")} > Forgot Password? */} e.preventDefault()} onContextMenu={(e) => e.preventDefault()} > {captcha} setInputCaptcha(e.currentTarget.value)} withAsterisk // disabled={otpRequired} disabled={isLogging || otpRequired} readOnly={isLogging} // mt="sm" /> {otpRequired && ( setOtp(e.currentTarget.value)} withAsterisk style={{ flex: 1 }} disabled={otpVerified} /> )}
{/* Carousel and Notes */} <IconShieldLockFilled />Security Notes : When you Login, Your User Id and Password travels in an encrypted and highly secured mode. For more information on Products and Services, Please Visit http://www.kccb.in/ {/* Footer */} © 2025 KCC Bank. All rights reserved. |{" "} Disclaimer |{" "} Privacy Policy |{" "} Phishing |{" "} Security Tips |{" "} Grievances
); }