-
Notifications
You must be signed in to change notification settings - Fork 23
added admin controllerwhere admin can delete user id #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 4 commits
2882be3
007797d
452c778
6a1ed2f
74e50e8
eb77279
a180767
61e1da1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,49 +20,74 @@ import ThemeContext from './assets/context/ThemeContext'; | |
| import useAuth from './auth/useAuth'; | ||
| import { ProfileProvider } from './assets/context/ProfileContext'; | ||
| import Network from './assets/components/network/Network'; | ||
| import Adminpage from './assets/components/Admin/Adminpage'; | ||
| import Usersprofile from './assets/components/Admin/Usersprofile'; | ||
| import EditUsersprofile from './assets/components/Admin/EditUsersprofile'; | ||
|
|
||
| function App() { | ||
|
|
||
| const { darkMode } = useContext(ThemeContext); | ||
| const { auth } = useAuth(); | ||
|
|
||
| return ( | ||
| <main className='w-full min-h-screen' data-theme={darkMode ? "dim" : "nord"}> | ||
| <div className='max-w-[2000px] mx-auto min-h-screen px-2 py-6 bg-base-200'> | ||
| <main | ||
| className="w-full min-h-screen" | ||
| data-theme={darkMode ? "dim" : "nord"} | ||
| > | ||
| <div className="max-w-[2000px] mx-auto min-h-screen px-2 py-6 bg-base-200"> | ||
| <Routes> | ||
| {/* <Route path='*' element={<NotFound />} /> */} | ||
| <Route path='*' element={<Navigate to='/' replace />} /> | ||
| <Route path='/unauthorized' element={<Unauthorized />} /> | ||
| <Route path="*" element={<Navigate to="/" replace />} /> | ||
| <Route path="/unauthorized" element={<Unauthorized />} /> | ||
|
|
||
| <Route element={<PersistLogin />}> | ||
| <Route element={<RedirectIfAuth />}> | ||
| <Route path='/' element={<Login />} /> | ||
| <Route path='/register' element={<Register />} /> | ||
| <Route path='/login' element={<Login />} /> | ||
| <Route path="/" element={<Login />} /> | ||
| <Route path="/register" element={<Register />} /> | ||
| <Route path="/login" element={<Login />} /> | ||
| </Route> | ||
| </Route> | ||
|
|
||
| {/* Protected Routes */} | ||
| <Route element={<PersistLogin />}> | ||
| <Route element={<RequireAuth allowedRoles={['owner', 'admin', 'moderator', 'user']} />}> | ||
| <Route | ||
| element={ | ||
| <RequireAuth | ||
| allowedRoles={["owner", "admin", "moderator", "user"]} | ||
| /> | ||
| } | ||
| > | ||
| <Route element={<Layout />}> | ||
| <Route path='/dashboard' element={<Dashboard />} /> | ||
| <Route path='/profile' element={<Navigate to={`/profile/${auth?.username}`} />} /> | ||
| <Route path="/dashboard" element={<Dashboard />} /> | ||
| <Route | ||
| path="/profile" | ||
| element={<Navigate to={`/profile/${auth?.username}`} />} | ||
| /> | ||
| <Route path="/profile/:username" element={<ProfileProvider />}> | ||
| <Route index element={<Profile />} /> | ||
| <Route path="edit" element={<EditProfile />} /> | ||
| </Route> | ||
| <Route path='/network' element={<Network />} /> | ||
| <Route path='/tester' element={<TesterComponent />} /> | ||
| <Route path="/network" element={<Network />} /> | ||
|
|
||
| <Route path="/tester" element={<TesterComponent />} /> | ||
| </Route> | ||
| </Route> | ||
|
|
||
| <Route element={<RequireAuth allowedRoles={["owner", "admin"]} />}> | ||
| <Route element={<Layout />}> | ||
| <Route path="/adminpage" element={<Adminpage />} /> | ||
| <Route path="/admin/profile/:username" element={<ProfileProvider />} > | ||
| <Route index element={<Usersprofile />} /> | ||
| <Route path="editbyadmin" element={<EditUsersprofile/>}/> | ||
|
||
| </Route> | ||
| </Route> | ||
| </Route> | ||
| </Route> | ||
| {/* End Protected Routes */} | ||
|
|
||
| </Routes> | ||
| </div> | ||
| </main> | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| export default App | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be removed as mentioned earlier |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import React, { useEffect, useState } from "react"; | ||
| import { Link } from "react-router-dom"; | ||
| import Loading from "../subcomponents/Loading"; | ||
| import useAuth from "../../../auth/useAuth"; | ||
| import { axiosPrivate } from "../../../api/axios"; | ||
| import { FaTrashAlt, FaUserCircle } from "react-icons/fa"; | ||
|
|
||
| const Adminpage = () => { | ||
| const { auth } = useAuth(); | ||
| const [users, setUsers] = useState([]); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| const [showModal, setShowModal] = useState(false); | ||
| const [userToDelete, setUserToDelete] = useState(null); | ||
| const [statusMessage, setStatusMessage] = useState(null); | ||
|
|
||
| const fetchUsers = async () => { | ||
| setLoading(true); | ||
| try { | ||
| const response = await axiosPrivate.get("/api/users", { | ||
| headers: { | ||
| Authorization: `Bearer ${auth?.accessToken}`, | ||
| }, | ||
| }); | ||
| setUsers(response.data); | ||
| } catch (err) { | ||
| console.error("Error fetching users:", err); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| fetchUsers(); | ||
| }, [auth?.accessToken]); | ||
|
|
||
| const confirmDelete = (user) => { | ||
| setUserToDelete(user); | ||
| setShowModal(true); | ||
| }; | ||
|
|
||
| const handleDeleteConfirmed = async () => { | ||
| if (!userToDelete) return; | ||
|
|
||
| try { | ||
| const res = await axiosPrivate.delete(`/api/users/${userToDelete._id}`, { | ||
| headers: { | ||
| Authorization: `Bearer ${auth?.accessToken}`, | ||
| }, | ||
| }); | ||
|
|
||
| if (res.status === 200 || res.status === 204) { | ||
| setStatusMessage({ | ||
| type: "success", | ||
| text: `User "${userToDelete.username}" deleted successfully.`, | ||
| }); | ||
| fetchUsers(); | ||
| } else { | ||
| throw new Error("Unexpected response status"); | ||
| } | ||
| } catch (err) { | ||
| console.error("Error deleting user:", err); | ||
| setStatusMessage({ | ||
| type: "error", | ||
| text: "Failed to delete user.", | ||
| }); | ||
| } finally { | ||
| setShowModal(false); | ||
| setUserToDelete(null); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="max-w-6xl mx-auto p-6 bg-base-100 rounded-xl shadow-lg mt-10"> | ||
| <h2 className="text-2xl font-semibold mb-6 text-primary">User Management</h2> | ||
|
|
||
|
|
||
| {statusMessage && ( | ||
| <div | ||
| className={`mb-4 p-3 rounded ${ | ||
| statusMessage.type === "success" | ||
| ? "bg-green-100 text-green-800" | ||
| : "bg-red-100 text-red-800" | ||
| }`} | ||
| > | ||
| {statusMessage.text} | ||
| </div> | ||
| )} | ||
|
|
||
| {loading ? ( | ||
| <Loading /> | ||
| ) : ( | ||
| <div className="grid gap-6 md:grid-cols-2"> | ||
| {users.map((user) => ( | ||
| <div | ||
| key={user._id} | ||
| className="p-5 bg-base-200 rounded-lg shadow-sm hover:shadow-md transition duration-300 flex flex-col justify-between" | ||
| > | ||
| <div className="mb-4"> | ||
| <p className="text-lg font-medium mb-1 flex items-center gap-2"> | ||
| <FaUserCircle className="text-primary text-xl" /> | ||
| {user.firstName} {user.lastName} | ||
| </p> | ||
| <p className="text-sm text-base-content/70 mb-1"> | ||
| <strong>Username:</strong> {user.username} | ||
| </p> | ||
| </div> | ||
|
|
||
| <div className="flex items-center gap-4 mt-2"> | ||
| <Link | ||
| to={`/admin/profile/${user.username}`} | ||
| className="text-primary hover:underline text-sm flex items-center gap-2" | ||
| > | ||
| <FaUserCircle /> | ||
| View Profile | ||
| </Link> | ||
|
|
||
| <button | ||
| onClick={() => confirmDelete(user)} | ||
| className="text-red-600 hover:text-red-800 text-sm flex items-center gap-2" | ||
| > | ||
| <FaTrashAlt /> | ||
| Delete User | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| )} | ||
|
|
||
| {/* Modal */} | ||
| {showModal && userToDelete && ( | ||
| <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50"> | ||
| <div className="bg-base-100 p-6 rounded-lg shadow-xl w-80"> | ||
| <h3 className="text-lg font-semibold text-center text-red-600 mb-4">Confirm Deletion</h3> | ||
| <p className="text-sm text-center mb-4"> | ||
| Are you sure you want to delete <strong>{userToDelete.username}</strong>? | ||
| This action cannot be undone. | ||
| </p> | ||
| <div className="flex justify-between mt-6"> | ||
| <button | ||
| onClick={() => setShowModal(false)} | ||
| className="px-4 py-2 rounded transition-colors bg-gray-200 text-gray-800 hover:bg-gray-300 dark:bg-gray-700 dark:text-white dark:hover:bg-gray-600" | ||
| > | ||
| Cancel | ||
| </button> | ||
|
Comment on lines
+136
to
+299
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Button is hard to read in dark theme |
||
| <button | ||
| onClick={handleDeleteConfirmed} | ||
| className="px-4 py-2 bg-red-600 text-white rounded hover:bg-red-700" | ||
| > | ||
| Delete | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Adminpage; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is redundant as well, the EditProfile page currently knows when the User or an Admin/Owner roles are viewing and will allow changes to happen. This seems to be a duplicate of Edit Profile and does not need to be added.
Furthermore, EditProfile is also being redesigned by another contribute in issue #86
Please remove this.