fix: Improve admin authentication flow and fix MUI Grid warnings
Some checks failed
ParentFlow CI/CD Pipeline / Backend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Frontend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Security Scanning (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-app/maternal-app-backend dockerfile:Dockerfile.production name:backend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-web dockerfile:Dockerfile.production name:frontend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Development (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled

Authentication fixes:
- Add isAuthenticated() method to ApiClient to check token presence
- Add useEffect in AdminLayout to redirect to login if no token found
- Fix logout to not fail when token is expired/invalid (401)
- Properly handle logout errors by clearing tokens locally
- Clear tokens and redirect to /login on 401 refresh token failure

UI/UX fixes:
- Replace all deprecated MUI Grid v1 with CSS Grid layout
- Remove Grid import since it's no longer used
- Fix Grid warnings: item, xs, sm, md props deprecated in MUI v7
- Use responsive CSS Grid with gridTemplateColumns for all layouts

Security improvements:
- Check authentication status on every page load
- Auto-redirect to login if no valid session exists
- Handle expired tokens gracefully without breaking logout flow
This commit is contained in:
Andrei
2025-10-08 08:30:22 +00:00
parent aca7061851
commit 28a781517c
3 changed files with 105 additions and 105 deletions

View File

@@ -3,7 +3,6 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { import {
Box, Box,
Grid,
Paper, Paper,
Typography, Typography,
Card, Card,
@@ -198,8 +197,7 @@ export default function DashboardPage() {
</Box> </Box>
{/* Stats Cards */} {/* Stats Cards */}
<Grid container spacing={3} sx={{ mb: 3 }}> <Box sx={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(250px, 1fr))', gap: 3, mb: 3 }}>
<Grid item xs={12} sm={6} md={3}>
<StatCard <StatCard
icon={<People />} icon={<People />}
title="Total Users" title="Total Users"
@@ -207,8 +205,6 @@ export default function DashboardPage() {
change={5.2} change={5.2}
color="primary" color="primary"
/> />
</Grid>
<Grid item xs={12} sm={6} md={3}>
<StatCard <StatCard
icon={<FamilyRestroom />} icon={<FamilyRestroom />}
title="Families" title="Families"
@@ -216,8 +212,6 @@ export default function DashboardPage() {
change={3.1} change={3.1}
color="secondary" color="secondary"
/> />
</Grid>
<Grid item xs={12} sm={6} md={3}>
<StatCard <StatCard
icon={<ChildCare />} icon={<ChildCare />}
title="Children" title="Children"
@@ -225,8 +219,6 @@ export default function DashboardPage() {
change={4.5} change={4.5}
color="info" color="info"
/> />
</Grid>
<Grid item xs={12} sm={6} md={3}>
<StatCard <StatCard
icon={<TrendingUp />} icon={<TrendingUp />}
title="Activities Today" title="Activities Today"
@@ -234,12 +226,10 @@ export default function DashboardPage() {
change={12.3} change={12.3}
color="success" color="success"
/> />
</Grid> </Box>
</Grid>
{/* Charts Row */} {/* Charts Row */}
<Grid container spacing={3} sx={{ mb: 3 }}> <Box sx={{ display: 'grid', gridTemplateColumns: { xs: '1fr', md: '2fr 1fr' }, gap: 3, mb: 3 }}>
<Grid item xs={12} md={8}>
<Paper sx={{ p: 3, minWidth: 500 }}> <Paper sx={{ p: 3, minWidth: 500 }}>
<Typography variant="h6" gutterBottom> <Typography variant="h6" gutterBottom>
User Growth (Last 30 Days) User Growth (Last 30 Days)
@@ -260,8 +250,6 @@ export default function DashboardPage() {
</AreaChart> </AreaChart>
</ResponsiveContainer> </ResponsiveContainer>
</Paper> </Paper>
</Grid>
<Grid item xs={12} md={4}>
<Paper sx={{ p: 3, minWidth: 500 }}> <Paper sx={{ p: 3, minWidth: 500 }}>
<Typography variant="h6" gutterBottom> <Typography variant="h6" gutterBottom>
Activity Distribution Activity Distribution
@@ -286,12 +274,10 @@ export default function DashboardPage() {
</PieChart> </PieChart>
</ResponsiveContainer> </ResponsiveContainer>
</Paper> </Paper>
</Grid> </Box>
</Grid>
{/* Recent Activity and System Status */} {/* Recent Activity and System Status */}
<Grid container spacing={3}> <Box sx={{ display: 'grid', gridTemplateColumns: { xs: '1fr', md: '1fr 1fr' }, gap: 3 }}>
<Grid item xs={12} md={6}>
<Paper sx={{ p: 3, minWidth: 500 }}> <Paper sx={{ p: 3, minWidth: 500 }}>
<Typography variant="h6" gutterBottom> <Typography variant="h6" gutterBottom>
Recent Users Recent Users
@@ -319,8 +305,6 @@ export default function DashboardPage() {
))} ))}
</List> </List>
</Paper> </Paper>
</Grid>
<Grid item xs={12} md={6}>
<Paper sx={{ p: 3, minWidth: 500 }}> <Paper sx={{ p: 3, minWidth: 500 }}>
<Typography variant="h6" gutterBottom> <Typography variant="h6" gutterBottom>
System Status System Status
@@ -376,8 +360,7 @@ export default function DashboardPage() {
</ListItem> </ListItem>
</List> </List>
</Paper> </Paper>
</Grid> </Box>
</Grid>
</Box> </Box>
</AdminLayout> </AdminLayout>
); );

View File

@@ -1,6 +1,6 @@
'use client'; 'use client';
import { useState, ReactNode } from 'react'; import { useState, useEffect, ReactNode } from 'react';
import { useRouter, usePathname } from 'next/navigation'; import { useRouter, usePathname } from 'next/navigation';
import { import {
Box, Box,
@@ -45,6 +45,13 @@ export default function AdminLayout({ children }: AdminLayoutProps) {
const [mobileOpen, setMobileOpen] = useState(false); const [mobileOpen, setMobileOpen] = useState(false);
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
// Check authentication on mount
useEffect(() => {
if (!apiClient.isAuthenticated()) {
router.push('/login');
}
}, [router]);
const handleDrawerToggle = () => { const handleDrawerToggle = () => {
setMobileOpen(!mobileOpen); setMobileOpen(!mobileOpen);
}; };

View File

@@ -134,12 +134,22 @@ class ApiClient {
async logout() { async logout() {
try { try {
// Only try to call logout endpoint if we have a token
if (this.token) {
await this.request('POST', '/auth/logout'); await this.request('POST', '/auth/logout');
}
} catch (error) {
// Ignore errors on logout - we'll clear tokens anyway
console.log('Logout request failed, clearing tokens locally');
} finally { } finally {
this.clearTokens(); this.clearTokens();
} }
} }
isAuthenticated(): boolean {
return !!this.token;
}
async getCurrentAdmin() { async getCurrentAdmin() {
return this.request('GET', '/auth/me'); return this.request('GET', '/auth/me');
} }