1- import { useState , useEffect } from 'react' ;
1+ import { useState , useEffect , useRef } from 'react' ;
22import { useParams , useNavigate } from 'react-router-dom' ;
33import api from '../utils/api' ;
44import toast from 'react-hot-toast' ;
@@ -8,6 +8,7 @@ import AuthHeader from '../components/Auth/AuthHeader';
88import SocialAuthConfig from '../components/Auth/SocialAuthConfig' ;
99import SocialAuthModal from '../components/Auth/SocialAuthModal' ;
1010import UserTable from '../components/Auth/UserTable' ;
11+ import Pagination from '../components/Database/Pagination' ;
1112import SectionHeader from '../components/Dashboard/SectionHeader' ;
1213import AddRecordDrawer from '../components/AddRecordDrawer' ;
1314import { PUBLIC_API_URL } from '../config' ;
@@ -24,6 +25,9 @@ export default function Auth() {
2425 const navigate = useNavigate ( ) ;
2526
2627 const [ users , setUsers ] = useState ( [ ] ) ;
28+ const [ page , setPage ] = useState ( 1 ) ;
29+ const [ limit , setLimit ] = useState ( 50 ) ;
30+ const [ totalRecords , setTotalRecords ] = useState ( 0 ) ;
2731 const [ loading , setLoading ] = useState ( true ) ;
2832 const [ searchTerm , setSearchTerm ] = useState ( '' ) ;
2933 const [ project , setProject ] = useState ( null ) ;
@@ -32,6 +36,7 @@ export default function Auth() {
3236 const [ isSocialAuthModalOpen , setIsSocialAuthModalOpen ] = useState ( false ) ;
3337 const [ isAddModalOpen , setIsAddModalOpen ] = useState ( false ) ;
3438 const [ editingUser , setEditingUser ] = useState ( null ) ; // user being edited
39+ const latestUsersRequestId = useRef ( 0 ) ;
3540 const [ selectedProvider , setSelectedProvider ] = useState ( 'github' ) ;
3641 const [ authProviders , setAuthProviders ] = useState ( {
3742 github : { enabled : false , clientId : '' , clientSecret : '' , hasClientSecret : false } ,
@@ -76,16 +81,26 @@ export default function Auth() {
7681 setProject ( projRes . data ) ;
7782 if ( projRes . data . authProviders ) setAuthProviders ( projRes . data . authProviders ) ;
7883 if ( projRes . data . isAuthEnabled ) {
79- const usersRes = await api . get ( `/api/projects/${ projectId } /collections/users/data` ) ;
84+ const requestId = ++ latestUsersRequestId . current ;
85+ const usersRes = await api . get (
86+ `/api/projects/${ projectId } /admin/users?page=${ page } &limit=${ limit } `
87+ ) ;
88+
89+ if ( ! isMounted || requestId !== latestUsersRequestId . current ) return ;
8090 setUsers ( normalizeUsersResponse ( usersRes . data ) ) ;
91+ setTotalRecords (
92+ usersRes . data ?. data ?. total ||
93+ usersRes . data ?. total ||
94+ normalizeUsersResponse ( usersRes . data ) . length
95+ ) ;
8196 }
8297 }
8398 } catch { toast . error ( "Failed to load auth details" ) ; }
8499 finally { if ( isMounted ) setLoading ( false ) ; }
85100 } ;
86101 fetchData ( ) ;
87102 return ( ) => { isMounted = false ; } ;
88- } , [ projectId ] ) ;
103+ } , [ projectId , page , limit ] ) ;
89104
90105 const handleEnableAuth = async ( ) => {
91106 if ( ! hasUserCollection ) return toast . error ( "Please create a 'users' collection first." ) ;
@@ -154,8 +169,20 @@ export default function Auth() {
154169 const handleDeleteUser = async ( userId ) => {
155170 if ( ! confirm ( 'Delete this user? This cannot be undone.' ) ) return ;
156171 try {
157- await api . delete ( `/api/projects/${ projectId } /collections/users/data/${ userId } ` ) ;
158- setUsers ( prev => normalizeUsersResponse ( prev ) . filter ( u => u . _id !== userId ) ) ;
172+ await api . delete ( `/api/projects/${ projectId } /admin/users/${ userId } ` ) ;
173+ setUsers ( prevUsers => {
174+ const nextUsers = normalizeUsersResponse ( prevUsers ) . filter (
175+ u => u . _id !== userId
176+ ) ;
177+
178+ if ( nextUsers . length === 0 ) {
179+ setPage ( prevPage => ( prevPage > 1 ? prevPage - 1 : prevPage ) ) ;
180+ }
181+
182+ return nextUsers ;
183+ } ) ;
184+
185+ setTotalRecords ( prev => Math . max ( prev - 1 , 0 ) ) ;
159186 toast . success ( 'User deleted' ) ;
160187 } catch ( err ) {
161188 toast . error ( err . response ?. data ?. message || err . response ?. data ?. error || 'Failed to delete user' ) ;
@@ -222,8 +249,18 @@ export default function Auth() {
222249 } else {
223250 await api . post ( `/api/projects/${ projectId } /admin/users` , userData ) ;
224251 toast . success ( 'User created successfully' ) ;
225- const usersRes = await api . get ( `/api/projects/${ projectId } /collections/users/data` ) ;
252+ const requestId = ++ latestUsersRequestId . current ;
253+ const usersRes = await api . get (
254+ `/api/projects/${ projectId } /admin/users?page=${ page } &limit=${ limit } `
255+ ) ;
256+
257+ if ( requestId !== latestUsersRequestId . current ) return ;
226258 setUsers ( normalizeUsersResponse ( usersRes . data ) ) ;
259+ setTotalRecords (
260+ usersRes . data ?. data ?. total ||
261+ usersRes . data ?. total ||
262+ normalizeUsersResponse ( usersRes . data ) . length
263+ ) ;
227264 }
228265 setIsAddModalOpen ( false ) ;
229266 setEditingUser ( null ) ;
@@ -295,6 +332,16 @@ export default function Auth() {
295332 onResetPassword = { ( u ) => { setResetPasswordUser ( u ) ; setNewPassword ( '' ) ; } }
296333 onDelete = { handleDeleteUser }
297334 />
335+ < Pagination
336+ total = { totalRecords }
337+ page = { page }
338+ limit = { limit }
339+ onPageChange = { ( p ) => setPage ( p ) }
340+ onLimitChange = { ( newLimit ) => {
341+ setLimit ( newLimit ) ;
342+ setPage ( 1 ) ;
343+ } }
344+ />
298345 </ div >
299346 </ div >
300347
0 commit comments