feat: Add login/logout audit logging (refs #10)
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>
This commit is contained in:
Eric Gullickson
2026-01-11 12:08:41 -06:00
parent cdfba3c1a8
commit fbde51b8fd
7 changed files with 121 additions and 13 deletions

View File

@@ -0,0 +1,46 @@
/**
* @ai-summary Custom logout hook with audit logging
* @ai-context Tracks logout event before Auth0 logout for audit trail
*/
import { useAuth0 } from '@auth0/auth0-react';
import { useCallback } from 'react';
/**
* Custom hook that wraps Auth0 logout with audit tracking.
* Calls /api/auth/track-logout before performing Auth0 logout.
* The audit call is fire-and-forget to ensure logout always completes.
*/
export const useLogout = () => {
const { logout: auth0Logout, getAccessTokenSilently } = useAuth0();
const logout = useCallback(async () => {
// Fire-and-forget audit call (don't block logout)
try {
const token = await getAccessTokenSilently({ cacheMode: 'on' as const });
if (token) {
// Use fetch directly to avoid axios interceptor issues during logout
fetch('/api/auth/track-logout', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
}).catch(() => {
// Silently ignore errors - don't block logout
});
}
} catch {
// Token not available - proceed with logout anyway
}
// Perform Auth0 logout
auth0Logout({
logoutParams: {
returnTo: window.location.origin,
},
});
}, [auth0Logout, getAccessTokenSilently]);
return { logout };
};