All checks were successful
Deploy to Staging / Build Images (pull_request) Successful in 4m42s
Deploy to Staging / Deploy to Staging (pull_request) Successful in 38s
Deploy to Staging / Verify Staging (pull_request) Successful in 7s
Deploy to Staging / Notify Staging Ready (pull_request) Successful in 6s
Deploy to Staging / Notify Staging Failure (pull_request) Has been skipped
Backend: - Add login event logging to getUserStatus() controller method - Create POST /auth/track-logout endpoint for logout tracking Frontend: - Create useLogout hook that wraps Auth0 logout with audit tracking - Update all logout locations to use the new hook (SettingsPage, Layout, MobileSettingsScreen, useDeletion) Login events are logged when the frontend calls /auth/user-status after Auth0 callback. Logout events are logged via fire-and-forget call to /auth/track-logout before Auth0 logout. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
/**
|
|
* @ai-summary React hooks for account deletion functionality
|
|
*/
|
|
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { useAuth0 } from '@auth0/auth0-react';
|
|
import { useLogout } from '../../../core/auth/useLogout';
|
|
import { profileApi } from '../api/profile.api';
|
|
import { RequestDeletionRequest } from '../types/profile.types';
|
|
import toast from 'react-hot-toast';
|
|
|
|
interface ApiError {
|
|
response?: {
|
|
data?: {
|
|
error?: string;
|
|
};
|
|
};
|
|
message?: string;
|
|
}
|
|
|
|
export const useDeletionStatus = () => {
|
|
const { isAuthenticated, isLoading } = useAuth0();
|
|
|
|
return useQuery({
|
|
queryKey: ['user-deletion-status'],
|
|
queryFn: async () => {
|
|
const response = await profileApi.getDeletionStatus();
|
|
return response.data;
|
|
},
|
|
enabled: isAuthenticated && !isLoading,
|
|
staleTime: 1 * 60 * 1000, // 1 minute
|
|
gcTime: 5 * 60 * 1000, // 5 minutes cache time
|
|
refetchOnWindowFocus: true,
|
|
refetchOnMount: true,
|
|
});
|
|
};
|
|
|
|
export const useRequestDeletion = () => {
|
|
const queryClient = useQueryClient();
|
|
const { logout } = useLogout();
|
|
|
|
return useMutation({
|
|
mutationFn: (data: RequestDeletionRequest) => profileApi.requestDeletion(data),
|
|
onSuccess: (response) => {
|
|
queryClient.invalidateQueries({ queryKey: ['user-deletion-status'] });
|
|
queryClient.invalidateQueries({ queryKey: ['user-profile'] });
|
|
toast.success(response.data.message || 'Account deletion scheduled');
|
|
|
|
// Logout after 2 seconds (with audit tracking)
|
|
setTimeout(() => {
|
|
logout();
|
|
}, 2000);
|
|
},
|
|
onError: (error: ApiError) => {
|
|
toast.error(error.response?.data?.error || 'Failed to request account deletion');
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useCancelDeletion = () => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: () => profileApi.cancelDeletion(),
|
|
onSuccess: (response) => {
|
|
queryClient.invalidateQueries({ queryKey: ['user-deletion-status'] });
|
|
queryClient.invalidateQueries({ queryKey: ['user-profile'] });
|
|
queryClient.setQueryData(['user-profile'], response.data.profile);
|
|
toast.success('Welcome back! Account deletion cancelled');
|
|
},
|
|
onError: (error: ApiError) => {
|
|
toast.error(error.response?.data?.error || 'Failed to cancel account deletion');
|
|
},
|
|
});
|
|
};
|