fix: mail plugin CSRF token handling and trailing slash

- Add CSRF token storage in sessionStorage (persists across reloads)
- Send X-CSRF-Token header on POST/PATCH/DELETE requests
- Store CSRF token on login, clear on logout
- Fix trailing slash in mail list API call (/mail/ -> /mail)
This commit is contained in:
Agent Zero
2026-07-15 01:54:11 +02:00
parent a75665c1e6
commit 0d882eaca5
3 changed files with 29 additions and 2 deletions
+6 -1
View File
@@ -1,5 +1,5 @@
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { apiPost, apiGet, apiPatch, apiDelete, apiClient } from './client';
import { apiPost, apiGet, apiPatch, apiDelete, apiClient, setCsrfToken } from './client';
import { useAuthStore } from '@/store/authStore';
export interface LoginPayload {
@@ -112,6 +112,10 @@ export function useLogin() {
onSuccess: (data: any) => {
setUser(data.user || data);
setError(null);
// Store CSRF token for subsequent unsafe requests
if (data.csrf_token) {
setCsrfToken(data.csrf_token);
}
},
onError: (error: any) => {
setError(error.message || 'Login failed');
@@ -126,6 +130,7 @@ export function useLogout() {
mutationFn: () => apiPost('/auth/logout'),
onSettled: () => {
logout();
setCsrfToken(null);
queryClient.clear();
},
});