✅ Added try-catch block around the API call ✅ Proper error logging and meaningful error messages users/page.tsx ✅ Removed unused cookies() import and call (it was already being called inside requireAuthOrRedirect()) ✅ Added try-catch block around the API call ✅ Proper error logging and meaningful error messages
This commit is contained in:
@@ -16,13 +16,18 @@ interface DeviceDTO {
|
|||||||
export default async function DeviceManagementPage() {
|
export default async function DeviceManagementPage() {
|
||||||
const user = await requireAuthOrRedirect();
|
const user = await requireAuthOrRedirect();
|
||||||
|
|
||||||
const res = await axiosServer.get('/admin/devices', {
|
try {
|
||||||
headers: {
|
const res = await axiosServer.get('/admin/devices', {
|
||||||
Authorization: `Bearer ${user.token}`,
|
headers: {
|
||||||
},
|
Authorization: `Bearer ${user.token}`,
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const devices: DeviceDTO[] = res.data;
|
const devices: DeviceDTO[] = res.data;
|
||||||
|
|
||||||
return <DeviceTableSection initialDevices={devices} />;
|
return <DeviceTableSection initialDevices={devices} />;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch devices:', error);
|
||||||
|
throw new Error(`Failed to load devices: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// src/app/admin/users/page.tsx
|
// src/app/admin/users/page.tsx
|
||||||
import { cookies } from 'next/headers';
|
|
||||||
import { requireAuthOrRedirect } from '@/lib/authServer';
|
import { requireAuthOrRedirect } from '@/lib/authServer';
|
||||||
import axiosServer from '@/lib/axiosServer';
|
import axiosServer from '@/lib/axiosServer';
|
||||||
import UserTableSection from '@/components/admin/UserTableSection';
|
import UserTableSection from '@/components/admin/UserTableSection';
|
||||||
@@ -19,18 +18,19 @@ interface UserDTO {
|
|||||||
|
|
||||||
export default async function UserManagementPage() {
|
export default async function UserManagementPage() {
|
||||||
const user = await requireAuthOrRedirect();
|
const user = await requireAuthOrRedirect();
|
||||||
const cookieStore = cookies();
|
|
||||||
// @ts-expect-error - cookies() is not actually async, type is misleading
|
|
||||||
const token = cookieStore.get('authToken')?.value;
|
|
||||||
|
|
||||||
|
try {
|
||||||
const res = await axiosServer.get('/admin/users', {
|
const res = await axiosServer.get('/admin/users', {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${user.token}`, // token is guaranteed to exist
|
Authorization: `Bearer ${user.token}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const users: UserDTO[] = res.data;
|
const users: UserDTO[] = res.data;
|
||||||
|
|
||||||
return <UserTableSection initialUsers={users} />;
|
return <UserTableSection initialUsers={users} />;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch users:', error);
|
||||||
|
throw new Error(`Failed to load users: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user