Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 302x 302x 302x 302x 302x 302x 302x 28x 28x 6x 302x 12x 12x 12x 6x 6x 6x 6x 6x 302x 102x 154x | import {type FormEvent, type ReactElement, useEffect, useState} from 'react';
import {Link as RouterLink, useNavigate} from 'react-router-dom';
import {Box, Button, CircularProgress, Container, Link, Paper, TextField, Typography} from '@mui/material';
import {useLoginMutation} from '../../services/authApi';
import {useAppDispatch, useAppSelector} from '../../store/hooks';
import {selectIsAuthenticated, setCredentials} from '../../features/auth/authSlice';
import {logger} from '../../utils/logger';
import ApiErrorAlert from '../../components/ApiErrorAlert';
// ============================================================================
// Component
// ============================================================================
export default function LoginPage(): ReactElement {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const navigate = useNavigate();
const dispatch = useAppDispatch();
const isAuthenticated = useAppSelector(selectIsAuthenticated);
const [login, {isLoading, error}] = useLoginMutation();
// Debug: Log when authentication state changes
useEffect(() => {
logger.debug('Auth state changed:', {isAuthenticated});
if (isAuthenticated) {
logger.debug('User is authenticated, should redirect to dashboard');
}
}, [isAuthenticated]);
const handleSubmit = (e: FormEvent): void => {
e.preventDefault();
logger.debug('Attempting login...');
login({username, password})
.unwrap()
.then((response): void => {
logger.debug('Login response received', {userId: response.user.userId});
dispatch(
setCredentials({
user: {
userId: response.user.userId,
username: response.user.username,
email: response.user.email,
role: response.user.role
},
accessToken: response.accessToken,
refreshToken: response.refreshToken
})
);
logger.debug('Credentials dispatched, navigating to dashboard');
navigate('/dashboard', {replace: true});
})
.catch((err: unknown): void => {
logger.error('Login failed', err);
});
};
return (
<Box
sx={{
minHeight: '100vh',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'background.default',
px: 2
}}
>
<Container maxWidth="sm">
<Paper
elevation={1}
sx={{
p: 4,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
borderRadius: 2,
border: 1,
borderColor: 'divider'
}}
>
{/* Logo/Brand */}
<Typography
component="h1"
variant="h4"
sx={{
mb: 1,
fontWeight: 700,
color: 'primary.main'
}}
>
{import.meta.env.VITE_APP_NAME}
</Typography>
<Typography variant="body2" color="text.secondary" sx={{mb: 3}}>
Secure access to your pooled investment account.
</Typography>
{/* Error Alert - using reusable component */}
<ApiErrorAlert
error={error}
fallbackMessage="Login failed. Please try again."
/>
{/* Login Form */}
<Box component="form" onSubmit={handleSubmit} sx={{width: '100%'}}>
<TextField
margin="normal"
required
fullWidth
id="username"
label="Username"
name="username"
autoComplete="username"
autoFocus
value={username}
onChange={(e): void => {
setUsername(e.target.value);
}}
disabled={isLoading}
slotProps={{
htmlInput: {
minLength: 3,
maxLength: 50
}
}}
/>
<TextField
margin="normal"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
value={password}
onChange={(e): void => {
setPassword(e.target.value);
}}
disabled={isLoading}
slotProps={{
htmlInput: {
minLength: 8,
maxLength: 72
}
}}
/>
<Button
type="submit"
fullWidth
variant="contained"
size="large"
disabled={isLoading}
sx={{mt: 3, mb: 2, py: 1.5}}
>
{isLoading ? <CircularProgress size={24} color="inherit"/> : 'Sign In'}
</Button>
<Box sx={{textAlign: 'center'}}>
<Link
component={RouterLink}
to="/register"
variant="body2"
underline="hover"
>
Don't have an account? Sign up
</Link>
</Box>
</Box>
</Paper>
{/* Footer */}
<Typography
variant="body2"
color="text.secondary"
align="center"
sx={{mt: 3}}
>
{'\u00A9'} 2025 Flowstate Investment Platform
</Typography>
</Container>
</Box>
);
} |