Skip to content

Commit

Permalink
🔥 Admin Login Update
Browse files Browse the repository at this point in the history
  • Loading branch information
nmdra committed Oct 11, 2024
1 parent 190a587 commit 6d9b150
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 56 deletions.
45 changes: 26 additions & 19 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,37 @@ const router = createBrowserRouter(

{/* delivery and driver Routes */}
<Route path="/" element={<DLayout />}>
<Route path="/manager" element={<AdminLogin manager={true} />} />
<Route element={<StaffPrivateRoute manager={true} />}>
<Route
path="/manager/approve-driver"
element={<DLApproveDriver />}
/>
<Route
path="/manager/approve-driver/:id"
element={<DLDriverAccept />}
/>
{/*<Route path="/manager/approve-driver/:id/send-email" element={<DLSendEmail />} />*/}
<Route path="/alldrivers" element={<DLALLdrivers />} />
<Route
path="/manager/dashboard"
element={<DLmanageDash />}
/>
<Route
path="/manager/view-driver/:id"
element={<DLViewDriver />}
/>

<Route
path="/manager/delivery/:id"
element={<DLViewDelivery />}
/>
</Route>
<Route
path="/register-driver"
element={<DLDriverRegistrationForm />}
/>
<Route path="/upload-image" element={<DLImageUpload />} />
<Route
path="/manager/approve-driver"
element={<DLApproveDriver />}
/>
<Route
path="/manager/approve-driver/:id"
element={<DLDriverAccept />}
/>
{/*<Route path="/manager/approve-driver/:id/send-email" element={<DLSendEmail />} />*/}
<Route path="/driver/login" element={<DLLogin />} />{' '}
{/* Driver Login Route */}
<Route
Expand All @@ -239,22 +256,12 @@ const router = createBrowserRouter(
path="/driver/profile/edit"
element={<DLEditProfile />}
/>
<Route path="/alldrivers" element={<DLALLdrivers />} />
<Route path="/manager/dashboard" element={<DLmanageDash />} />
<Route
path="/manager/view-driver/:id"
element={<DLViewDriver />}
/>
<Route path="/driver/logout" element={<DLlogout />} />
<Route path="/driver/edit/:id" element={<DLeditdriver />} />
<Route path="/a" element={<OrderForm />} />
<Route path="/b" element={<OrderTable />} />
<Route path="/d" element={<Od />} />
<Route path="/DLAllDeliveries" element={<DLAllDeliveries />} />
<Route
path="/manager/delivery/:id"
element={<DLViewDelivery />}
/>
<Route path="/ongoing" element={<OngoingDelivery />} />
<Route
path="/driver/delivery/:id"
Expand Down
22 changes: 14 additions & 8 deletions frontend/src/Components/Admin/AstaffPrivateRoute.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Navigate, Outlet } from 'react-router-dom'
import { Navigate, Outlet } from 'react-router-dom';

const StaffPrivateRoute = () => {
// Check if user is authenticated (e.g., by checking for a token in local storage)
const staff = localStorage.getItem('staffToken') // Replace with your actual logic
const StaffPrivateRoute = ({ manager }) => { // Destructure manager from props

Check failure on line 3 in frontend/src/Components/Admin/AstaffPrivateRoute.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/Admin/AstaffPrivateRoute.jsx#L3

[react/prop-types] 'manager' is missing in props validation
// Check if the user is authenticated by checking for a token in local storage
const staff = localStorage.getItem('staffToken'); // Replace with your actual logic

// Log the user state for debugging
console.log(staff)
console.log(staff);

return staff ? <Outlet /> : <Navigate to="/Admin" replace />
}
// If staff token exists and manager prop is true, redirect to /manager
if (manager) {
return staff ? <Outlet /> : <Navigate to="/manager" replace />;
}

export default StaffPrivateRoute
// If staff token exists, render the Outlet, otherwise navigate to /Admin
return staff ? <Outlet /> : <Navigate to="/Admin" replace />;
};

export default StaffPrivateRoute;
60 changes: 31 additions & 29 deletions frontend/src/Components/Admin/admnlogins.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { ToastContainer, toast } from 'react-toastify'
import 'react-toastify/dist/ReactToastify.css'
import farmcartLogo from '../../assets/logo.png' // Update with correct path if needed
import React, { useState } from 'react';

Check failure on line 1 in frontend/src/Components/Admin/admnlogins.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/Admin/admnlogins.jsx#L1

[no-unused-vars] 'React' is defined but never used.
import { useNavigate } from 'react-router-dom';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import farmcartLogo from '../../assets/logo.png'; // Update with correct path if needed

const AdminLogin = () => {
const navigate = useNavigate()
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState('')
const AdminLogin = ({ manager }) => { // Receive the manager prop

Check failure on line 7 in frontend/src/Components/Admin/admnlogins.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/Admin/admnlogins.jsx#L7

[react/prop-types] 'manager' is missing in props validation
const navigate = useNavigate();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState('');

// Hardcoded usernames and passwords for three users
const users = [
Expand All @@ -24,30 +24,35 @@ const AdminLogin = () => {
email: 'admin3@example.com',
password: 'securePass',
},
]
];

// Login handler
const handleLogin = (e) => {
e.preventDefault()
e.preventDefault();

// Find matching user from the list
const user = users.find(
(user) => user.email === email && user.password === password
)
);

if (user) {
// If user exists, save token and navigate to /Admin
const token =
'd0ahfFiO0dPMd1StZ0W7fqYhxxuIJYtEDXgi6t39Pp2J2qaWyfcFT0gJKO3iT6pz' // Replace with actual token logic
localStorage.setItem('staffToken', token) // Save token in local storage
navigate('/admindashboard')
toast.success('Login successful!')
// If user exists, save token and navigate based on manager prop
const token = 'd0ahfFiO0dPMd1StZ0W7fqYhxxuIJYtEDXgi6t39Pp2J2qaWyfcFT0gJKO3iT6pz'; // Replace with actual token logic
localStorage.setItem('staffToken', token); // Save token in local storage

// Navigate based on the manager prop
if (manager) {
navigate('/manager/dashboard');
} else {
navigate('/admindashboard');
}
toast.success('Login successful!');
} else {
// If no match, show error message
setError('Invalid email or password.')
toast.error('Invalid email or password.')
setError('Invalid email or password.');
toast.error('Invalid email or password.');
}
}
};

return (
<div className="min-h-screen flex items-center justify-center bg-gray-100">
Expand Down Expand Up @@ -77,10 +82,7 @@ const AdminLogin = () => {
/>
</div>
<div className="mb-6">
<label
htmlFor="password"
className="block text-gray-700"
>
<label htmlFor="password" className="block text-gray-700">
Password
</label>
<input
Expand All @@ -103,7 +105,7 @@ const AdminLogin = () => {
</form>
</div>
</div>
)
}
);
};

export default AdminLogin
export default AdminLogin;

0 comments on commit 6d9b150

Please sign in to comment.