fix(admin): add CSRF token to admin user management operations (#1065)
Fixes #1064 Admin user operations (create, update, delete, toggle registration) were failing with "CSRF token missing" error when accessed behind a reverse proxy. This happened because the frontend was using plain fetch() instead of fetchWithCsrf() utility which automatically includes the CSRF token header. Changed all state-changing operations in AdminUsersPage to use fetchWithCsrf: - createAdminUser (POST /admin/users) - updateAdminUser (PUT /admin/users/:id) - deleteAdminUser (DELETE /admin/users/:id) - toggleRegistration (POST /admin/toggle-registration)
This commit is contained in:
parent
517a2ee7ea
commit
aafb1877ae
1 changed files with 5 additions and 4 deletions
|
|
@ -10,6 +10,7 @@ import {
|
||||||
import ConfirmDialog from '../Shared/ConfirmDialog';
|
import ConfirmDialog from '../Shared/ConfirmDialog';
|
||||||
import { getApiPath } from '../../config/paths';
|
import { getApiPath } from '../../config/paths';
|
||||||
import { useToast } from '../Shared/ToastContext';
|
import { useToast } from '../Shared/ToastContext';
|
||||||
|
import { fetchWithCsrf } from '../../utils/csrfService';
|
||||||
|
|
||||||
interface AdminUserItem {
|
interface AdminUserItem {
|
||||||
id: number;
|
id: number;
|
||||||
|
|
@ -43,7 +44,7 @@ const createAdminUser = async (
|
||||||
surname?: string,
|
surname?: string,
|
||||||
role?: 'admin' | 'user'
|
role?: 'admin' | 'user'
|
||||||
): Promise<AdminUserItem> => {
|
): Promise<AdminUserItem> => {
|
||||||
const res = await fetch(getApiPath('admin/users'), {
|
const res = await fetchWithCsrf(getApiPath('admin/users'), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
headers: {
|
headers: {
|
||||||
|
|
@ -84,7 +85,7 @@ const updateAdminUser = async (
|
||||||
const body: any = { email, name, surname, role };
|
const body: any = { email, name, surname, role };
|
||||||
if (password) body.password = password;
|
if (password) body.password = password;
|
||||||
|
|
||||||
const res = await fetch(getApiPath(`admin/users/${id}`), {
|
const res = await fetchWithCsrf(getApiPath(`admin/users/${id}`), {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
headers: {
|
headers: {
|
||||||
|
|
@ -116,7 +117,7 @@ const updateAdminUser = async (
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteAdminUser = async (id: number, t: any): Promise<void> => {
|
const deleteAdminUser = async (id: number, t: any): Promise<void> => {
|
||||||
const res = await fetch(getApiPath(`admin/users/${id}`), {
|
const res = await fetchWithCsrf(getApiPath(`admin/users/${id}`), {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
headers: { Accept: 'application/json' },
|
headers: { Accept: 'application/json' },
|
||||||
|
|
@ -466,7 +467,7 @@ const AdminUsersPage: React.FC = () => {
|
||||||
// Toggle registration
|
// Toggle registration
|
||||||
const toggleRegistration = async () => {
|
const toggleRegistration = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(getApiPath('admin/toggle-registration'), {
|
const res = await fetchWithCsrf(getApiPath('admin/toggle-registration'), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
headers: {
|
headers: {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue