Initial commit of react-revamp for Auditor Portal
This commit is contained in:
163
src/pages/account/Login.tsx
Normal file
163
src/pages/account/Login.tsx
Normal file
@@ -0,0 +1,163 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Box, Button, TextField, Typography, Container, Alert } from '@mui/material';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { accountService } from '../../api/accountService';
|
||||
import { DbSelectionDialog } from './DbSelectionDialog';
|
||||
|
||||
export const Login: React.FC = () => {
|
||||
const [username, setUsername] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [isInvalid, setIsInvalid] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [dialogOpen, setDialogOpen] = useState(false);
|
||||
const [tempUserData, setTempUserData] = useState<any>(null);
|
||||
|
||||
const { login, isAuthenticated } = useAuthStore();
|
||||
const navigate = useNavigate();
|
||||
|
||||
// If already logged in, redirect
|
||||
useEffect(() => {
|
||||
if (isAuthenticated) {
|
||||
navigate('/activity-feeds', { replace: true });
|
||||
}
|
||||
}, [isAuthenticated, navigate]);
|
||||
|
||||
const handleLogin = async () => {
|
||||
setIsInvalid(false);
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const userData = await accountService.login(username, password);
|
||||
|
||||
if (!userData) {
|
||||
setIsInvalid(true);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const roles = userData.user_roles ? userData.user_roles.toLowerCase().split(',') : [];
|
||||
|
||||
if (roles.includes('sr_auditor')) {
|
||||
// Open DB Selection Dialog
|
||||
setTempUserData(userData);
|
||||
setDialogOpen(true);
|
||||
} else if (roles.includes('auditor')) {
|
||||
// Direct login
|
||||
login(userData);
|
||||
navigate('/activity-feeds', { replace: true });
|
||||
} else if (roles.includes('video_uploader')) {
|
||||
// Direct login for dashboard
|
||||
login(userData);
|
||||
navigate('/activity-feeds', { replace: true });
|
||||
} else {
|
||||
alert("Your account is not configured to use this website. Please contact your admin for more info.");
|
||||
}
|
||||
} catch (error) {
|
||||
setIsInvalid(true);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDbSelection = (selectedOrg?: any) => {
|
||||
setDialogOpen(false);
|
||||
if (selectedOrg && tempUserData) {
|
||||
// Merge selected org details into user object
|
||||
const finalUserData = {
|
||||
...tempUserData,
|
||||
org_id: selectedOrg.org_id,
|
||||
db_name: selectedOrg.db_name,
|
||||
selected_org_name: selectedOrg.org_name
|
||||
};
|
||||
login(finalUserData);
|
||||
navigate('/activity-feeds', { replace: true });
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Container component="main" maxWidth="xs">
|
||||
<Box sx={{ marginTop: 8, display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
|
||||
|
||||
{/* Placeholder for Logo */}
|
||||
<Box sx={{ mb: 6, display: 'flex', justifyContent: 'center' }}>
|
||||
<img src="/images/SeekRightLogo.png" alt="Logo" width="250" onError={(e) => { e.currentTarget.style.display = 'none'; }} />
|
||||
</Box>
|
||||
|
||||
<Box component="form" sx={{ mt: 1, width: '100%' }}>
|
||||
<TextField
|
||||
margin="normal"
|
||||
required
|
||||
fullWidth
|
||||
id="username"
|
||||
placeholder="Admin"
|
||||
name="username"
|
||||
autoFocus
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
onKeyDown={(e) => e.key === 'Enter' && handleLogin()}
|
||||
InputProps={{
|
||||
sx: { borderRadius: '50px', '& fieldset': { borderColor: '#738ab8' } }
|
||||
}}
|
||||
sx={{
|
||||
mb: 1,
|
||||
'& .MuiInputBase-input': { padding: '10px 14px', fontSize: '0.9rem', color: '#333' }
|
||||
}}
|
||||
/>
|
||||
<TextField
|
||||
margin="normal"
|
||||
required
|
||||
fullWidth
|
||||
name="password"
|
||||
placeholder="Password"
|
||||
type="password"
|
||||
id="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
onKeyDown={(e) => e.key === 'Enter' && handleLogin()}
|
||||
InputProps={{
|
||||
sx: { borderRadius: '50px', '& fieldset': { borderColor: '#738ab8' } }
|
||||
}}
|
||||
sx={{
|
||||
mt: 1,
|
||||
mb: 3,
|
||||
'& .MuiInputBase-input': { padding: '10px 14px', fontSize: '0.9rem', color: '#333' }
|
||||
}}
|
||||
/>
|
||||
|
||||
<Box sx={{ display: 'flex', justifyContent: 'center', mt: 3, mb: 4 }}>
|
||||
<Button
|
||||
variant="contained"
|
||||
disabled={!username || !password || loading}
|
||||
onClick={handleLogin}
|
||||
sx={{
|
||||
borderRadius: '50px',
|
||||
backgroundColor: '#738ab8',
|
||||
width: '140px',
|
||||
pt: 1, pb: 1,
|
||||
boxShadow: 'none',
|
||||
'&:hover': {
|
||||
backgroundColor: '#5c729c',
|
||||
boxShadow: 'none',
|
||||
}
|
||||
}}
|
||||
>
|
||||
{loading ? '...' : 'LOGIN'}
|
||||
</Button>
|
||||
</Box>
|
||||
|
||||
|
||||
|
||||
{isInvalid && (
|
||||
<Alert severity="error" sx={{ mt: 2 }}>
|
||||
Invalid Credentials
|
||||
</Alert>
|
||||
)}
|
||||
</Box>
|
||||
</Box>
|
||||
|
||||
{/* Database Selection Dialog for SR_AUDITOR */}
|
||||
<DbSelectionDialog open={dialogOpen} onClose={handleDbSelection} />
|
||||
</Container>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user