feat: Create login page for admin.

wip: Admin user Configuration
This commit is contained in:
2025-08-08 14:57:33 +05:30
parent 3ee40cad55
commit b2e84608c3
13 changed files with 945 additions and 18 deletions

View File

@@ -14,12 +14,44 @@ export default function RootLayout({ children }: { children: React.ReactNode })
const pathname = usePathname();
const [authorized, SetAuthorized] = useState<boolean | null>(null);
const [userLastLoginDetails, setUserLastLoginDetails] = useState(null);
const [custname, setCustname] = useState<string | null>(null);
async function handleLogout(e: React.FormEvent) {
e.preventDefault();
localStorage.removeItem("access_token");
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}`
},
});
const data = await response.json();
if (response.ok && Array.isArray(data)) {
if (data.length > 0) {
const name = data[0].custname;
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,
});
}
}
useEffect(() => {
const token = localStorage.getItem("access_token");
if (!token) {
@@ -28,6 +60,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
}
else {
SetAuthorized(true);
handleFetchUserName();
}
}, []);
@@ -44,8 +77,8 @@ export default function RootLayout({ children }: { children: React.ReactNode })
const data = await response.json();
if (response.ok) {
return data;
}
else if(response.status ===401 ||data.message === 'invalid or expired token'){
}
else if (response.status === 401 || data.message === 'invalid or expired token') {
// console.log(data);
localStorage.removeItem("access_token");
router.push('/login');
@@ -125,7 +158,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
>
<Stack gap={0} align="flex-start">
<Title order={4} style={{ fontFamily: "inter", fontSize: '22px' }}>
Welcome, Rajat Kumar Maharana
Welcome, {custname ?? null}
</Title>
<Text size="xs" c="gray" style={{ fontFamily: "inter", fontSize: '13px' }}>
Last logged in at {userLastLoginDetails ? new Date(userLastLoginDetails).toLocaleString() : "N/A"}