feat : fetch mobile number by api
This commit is contained in:
@@ -9,6 +9,7 @@ import logo from '@/app/image/logo1.jpg';
|
||||
import NextImage from 'next/image';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import { useDisclosure, useMediaQuery } from '@mantine/hooks';
|
||||
import { fetchAndStoreUserName } from '../_util/userdetails';
|
||||
|
||||
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
const router = useRouter();
|
||||
@@ -38,51 +39,6 @@ export default function RootLayout({ children }: { children: React.ReactNode })
|
||||
router.push("/login");
|
||||
}
|
||||
|
||||
async function handleFetchUserName() {
|
||||
try {
|
||||
const token = localStorage.getItem("access_token");
|
||||
const response = await fetch('/api/customer', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
},
|
||||
});
|
||||
if (!response.ok) {
|
||||
notifications.show({
|
||||
withBorder: true,
|
||||
color: "red",
|
||||
title: "Error",
|
||||
message: "Internal Server Error",
|
||||
autoClose: 5000,
|
||||
});
|
||||
localStorage.removeItem("access_token");
|
||||
localStorage.removeItem("remitter_name");
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
if (response.ok && Array.isArray(data)) {
|
||||
if (data.length > 0) {
|
||||
const name = data[0].custname;
|
||||
const mobileNumber = data[0].mobileno;
|
||||
localStorage.setItem("remitter_name", name);
|
||||
localStorage.setItem("remitter_mobile_no", mobileNumber);
|
||||
setCustname(name);
|
||||
}
|
||||
} else {
|
||||
throw new Error();
|
||||
}
|
||||
} catch {
|
||||
notifications.show({
|
||||
withBorder: true,
|
||||
color: "red",
|
||||
title: "Please try again later",
|
||||
message: "Unable to Fetch, Please try again later",
|
||||
autoClose: 5000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// When reload and click on back then logout
|
||||
useEffect(() => {
|
||||
// Push fake history state to trap navigation
|
||||
@@ -113,7 +69,10 @@ export default function RootLayout({ children }: { children: React.ReactNode })
|
||||
}
|
||||
else {
|
||||
SetAuthorized(true);
|
||||
handleFetchUserName();
|
||||
fetchAndStoreUserName(token).then(() => {
|
||||
const name = localStorage.getItem("remitter_name");
|
||||
if (name) setCustname(name);
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ interface SendOtpPayload {
|
||||
|
||||
function getStoredMobileNumber(): string | null {
|
||||
// const mobileNumber = localStorage.getItem('remitter_mobile_no');
|
||||
const mobileNumber = "6297421727";
|
||||
const mobileNumber = "7890544527";
|
||||
if (!mobileNumber) {
|
||||
notifications.show({
|
||||
title: 'Missing Mobile Number',
|
||||
|
||||
48
src/app/_util/userdetails.ts
Normal file
48
src/app/_util/userdetails.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { notifications } from "@mantine/notifications";
|
||||
|
||||
export async function fetchAndStoreUserName(token: string) {
|
||||
if (!token) return;
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/customer", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
notifications.show({
|
||||
withBorder: true,
|
||||
color: "red",
|
||||
title: "Error",
|
||||
message: "Internal Server Error",
|
||||
autoClose: 5000,
|
||||
});
|
||||
localStorage.removeItem("remitter_name");
|
||||
localStorage.removeItem("remitter_mobile_no");
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
if (Array.isArray(data) && data.length > 0) {
|
||||
const { custname, mobileno } = data[0];
|
||||
localStorage.setItem("remitter_name", custname);
|
||||
localStorage.setItem("remitter_mobile_no", mobileno);
|
||||
}
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
localStorage.removeItem("remitter_name");
|
||||
localStorage.removeItem("remitter_mobile_no");
|
||||
console.error('Error sending OTP:', error.response?.data || error.message);
|
||||
notifications.show({
|
||||
withBorder: true,
|
||||
color: "red",
|
||||
title: "Please try again later",
|
||||
message: "Unable to Fetch, Please try again later",
|
||||
autoClose: 5000,
|
||||
});
|
||||
throw error.response?.data || error;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import { generateCaptcha } from '@/app/captcha';
|
||||
import { IconRefresh, IconShieldLockFilled } from "@tabler/icons-react";
|
||||
import dayjs from "dayjs";
|
||||
import { IconTrash } from "@tabler/icons-react";
|
||||
import { fetchAndStoreUserName } from "../_util/userdetails";
|
||||
|
||||
|
||||
export default function Login() {
|
||||
@@ -46,7 +47,8 @@ export default function Login() {
|
||||
}
|
||||
try {
|
||||
console.log(CIF);
|
||||
await sendOtp({ type: 'LOGIN_OTP', username: CIF, mobileNumber: mobile });
|
||||
// await sendOtp({ type: 'LOGIN_OTP', username: CIF, mobileNumber: mobile });
|
||||
await sendOtp({ type: 'LOGIN_OTP', username: CIF, mobileNumber: '7890544527' });
|
||||
return true;
|
||||
}
|
||||
catch (err: any) {
|
||||
@@ -62,7 +64,8 @@ export default function Login() {
|
||||
async function handleVerifyOtp(mobile?: string) {
|
||||
try {
|
||||
if (mobile) {
|
||||
await verifyLoginOtp(otp, mobile);
|
||||
// await verifyLoginOtp(otp, mobile);
|
||||
await verifyLoginOtp(otp, '7890544527');
|
||||
return true;
|
||||
}
|
||||
} catch {
|
||||
@@ -422,10 +425,7 @@ export default function Login() {
|
||||
localStorage.setItem("access_token", token);
|
||||
localStorage.setItem("pswExpiryDate", data.loginPswExpiry);
|
||||
|
||||
if (
|
||||
data.loginPswExpiry &&
|
||||
dayjs(data.loginPswExpiry).diff(dayjs(), "day") < 0
|
||||
) {
|
||||
if (data.loginPswExpiry && dayjs(data.loginPswExpiry).diff(dayjs(), "day") < 0) {
|
||||
notifications.show({
|
||||
withBorder: true,
|
||||
color: "orange",
|
||||
@@ -437,6 +437,8 @@ export default function Login() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetching mobile no and user name
|
||||
await fetchAndStoreUserName(token);
|
||||
if (data.FirstTimeLogin === true) {
|
||||
router.push("/SetPassword");
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user