feat : Otp verification Feature active.
This commit is contained in:
@@ -294,6 +294,7 @@ export default function ViewBeneficiary() {
|
||||
value={otp}
|
||||
onChange={(e) => setOtp(e.currentTarget.value)}
|
||||
placeholder="Enter OTP"
|
||||
maxLength={6}
|
||||
/>
|
||||
<Group justify="space-between" mt="xs">
|
||||
{/* Resend OTP Timer or Icon */}
|
||||
|
||||
@@ -100,7 +100,7 @@ export default function Home() {
|
||||
setTimeout(() => {
|
||||
router.push(`/accounts/account_statement?accNo=${accountNo}`);
|
||||
setLoadingAccountNo(null);
|
||||
}, 5000);
|
||||
}, 6000);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import axios from 'axios';
|
||||
|
||||
interface SendOtpPayload {
|
||||
mobileNumber?: string;
|
||||
type: string;
|
||||
@@ -12,73 +9,70 @@ interface SendOtpPayload {
|
||||
ref?: string;
|
||||
date?: string;
|
||||
userOtp?: string;
|
||||
username?: string
|
||||
username?: string;
|
||||
}
|
||||
|
||||
function getStoredMobileNumber(): string | null {
|
||||
function getStoredMobileNumber(): string {
|
||||
const mobileNumber = localStorage.getItem('remitter_mobile_no');
|
||||
// const mobileNumber = "7890544527";
|
||||
if (!mobileNumber) {
|
||||
notifications.show({
|
||||
title: 'Missing Mobile Number',
|
||||
message: 'Mobile number not found. Please re-login or update your profile.',
|
||||
color: 'red',
|
||||
});
|
||||
return null;
|
||||
}
|
||||
if (!mobileNumber) throw new Error('Mobile number not found.');
|
||||
return mobileNumber;
|
||||
}
|
||||
|
||||
export async function sendOtp(payload: SendOtpPayload) {
|
||||
try {
|
||||
const mobileNumber = payload.mobileNumber || getStoredMobileNumber();
|
||||
const response = await fetch('api/otp/send', {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ ...payload,mobileNumber}),
|
||||
})
|
||||
console.log('otp sended.');
|
||||
return await response.json();
|
||||
} catch (error: any) {
|
||||
console.error('Error sending OTP:', error.response?.data || error.message);
|
||||
throw error.response?.data || error;
|
||||
const mobileNumber = payload.mobileNumber || getStoredMobileNumber();
|
||||
|
||||
const response = await fetch('/api/otp/send', {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ ...payload, mobileNumber }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.log('OTP sended failed.');
|
||||
const errorMsg = data?.error || 'Send OTP failed. Please try again later.';
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
console.log('OTP sended.');
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function verifyOtp(otp: string) {
|
||||
try {
|
||||
const mobileNumber = getStoredMobileNumber();
|
||||
const response = await fetch(`/api/otp/verify?mobileNumber=${mobileNumber}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({otp}),
|
||||
})
|
||||
console.log('Otp verified');
|
||||
return await response.json();
|
||||
} catch (error: any) {
|
||||
console.error('Error verifying OTP:', error.response?.data || error.message);
|
||||
throw error.response?.data || error;
|
||||
const mobileNumber = getStoredMobileNumber();
|
||||
|
||||
const response = await fetch(`/api/otp/verify?mobileNumber=${mobileNumber}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ otp }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.log('OTP not verified.');
|
||||
const errorMsg = data?.error || 'OTP verification failed.';
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
console.log('OTP verified.');
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function verifyLoginOtp(otp: string, mobileNumber: string) {
|
||||
try {
|
||||
// const mobileNumber = getStoredMobileNumber();
|
||||
const response = await fetch(`/api/otp/verify?mobileNumber=${mobileNumber}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({otp}),
|
||||
})
|
||||
console.log('Otp verified.');
|
||||
return await response.json();
|
||||
} catch (error: any) {
|
||||
console.error('Error verifying OTP:', error.response?.data || error.message);
|
||||
throw error.response?.data || error;
|
||||
const response = await fetch(`/api/otp/verify?mobileNumber=${mobileNumber}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ otp }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.log('Login OTP not verified.');
|
||||
const errorMsg = data?.error || 'OTP verification failed.';
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
console.log("Login OTP verified");
|
||||
return data;
|
||||
}
|
||||
@@ -35,30 +35,29 @@ export default function Login() {
|
||||
const [mobile, setMobile] = useState("");
|
||||
|
||||
async function handleSendOtp(mobile?: string) {
|
||||
// console.log("hi mobile", mobile);
|
||||
if (!mobile) {
|
||||
notifications.show({
|
||||
title: 'Error',
|
||||
message: 'Mobile number not found.Contact to administrator',
|
||||
message: 'Mobile number not found. Contact administrator.',
|
||||
color: 'red',
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// console.log(CIF);
|
||||
await sendOtp({ type: 'LOGIN_OTP', username: CIF, mobileNumber: mobile });
|
||||
// await sendOtp({ type: 'LOGIN_OTP', username: CIF, mobileNumber: '7890544527' });
|
||||
// await sendOtp({ type: 'LOGIN_OTP', username: CIF, mobileNumber: "7890544527" });
|
||||
notifications.show({
|
||||
color: "orange",
|
||||
title: "OTP Required",
|
||||
message: "OTP sent to your registered mobile number",
|
||||
color: 'orange',
|
||||
title: 'OTP Required',
|
||||
message: 'OTP sent to your registered mobile number.',
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (err: any) {
|
||||
} catch (err: any) {
|
||||
notifications.show({
|
||||
title: 'Error',
|
||||
message: `${err.error}.SMS vendor facing some issue.Please try again later.` || 'Send OTP failed.Please try again later.',
|
||||
message: err?.message || 'Send OTP failed. Please try again later.',
|
||||
color: 'red',
|
||||
});
|
||||
return false;
|
||||
@@ -72,11 +71,18 @@ export default function Login() {
|
||||
// await verifyLoginOtp(otp, '7890544527');
|
||||
return true;
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
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();
|
||||
@@ -195,13 +201,6 @@ export default function Login() {
|
||||
setButtonLabel("Verify OTP");
|
||||
|
||||
const otpSent = await handleSendOtp(data.mobile);
|
||||
// if (otpSent) {
|
||||
// notifications.show({
|
||||
// color: "orange",
|
||||
// title: "OTP Required",
|
||||
// message: "OTP sent to your registered mobile number",
|
||||
// });
|
||||
// }
|
||||
setIsLogging(false);
|
||||
return;
|
||||
}
|
||||
@@ -271,17 +270,12 @@ export default function Login() {
|
||||
|
||||
const verified = await handleVerifyOtp(mobile);
|
||||
if (!verified) {
|
||||
notifications.show({
|
||||
title: "Invalid OTP",
|
||||
message: "The OTP entered does not match",
|
||||
color: "red",
|
||||
});
|
||||
setIsLogging(false);
|
||||
return;
|
||||
}
|
||||
|
||||
notifications.show({
|
||||
color: "green",
|
||||
color: "Blue",
|
||||
title: "OTP Verified",
|
||||
message: "Please click Login to continue",
|
||||
});
|
||||
@@ -583,6 +577,7 @@ export default function Login() {
|
||||
label="Enter OTP"
|
||||
placeholder="Enter OTP"
|
||||
value={otp}
|
||||
maxLength={6}
|
||||
onChange={(e) => setOtp(e.currentTarget.value)}
|
||||
withAsterisk
|
||||
style={{ flex: 1 }}
|
||||
|
||||
Reference in New Issue
Block a user