Files
Deep-Package-Inspection/docs/log/2026-07-30-1004-n.md

1.7 KiB

Iteration Log: Full Target User Account Impersonation in View-As Mode

  • Requested: Implement full target user account impersonation for the "View-As" feature so that admins inspect the dashboard 100% as the target user account (role, site_uuid, agent_uuids, company_name), with instant session destruction on logout or exit.

  • Workflow: Test-Driven Development (TDD) Workflow.

  • Steps Taken:

    1. Created TDD test test/view_as_full_impersonation_test.js to assert target user metadata adoption and backend middleware user lookup.
    2. Ran node test/view_as_full_impersonation_test.js -> Confirmed RED failure phase as expected.
    3. Modified backend/routes/auth/viewAs.js to accept target_user_id and target_username and generate view-as tokens containing target user identity.
    4. Modified backend/middleware/auth.js to look up User.findById(viewDecoded.target_user_id) and adopt the target user's exact role, site_uuid, agent_uuids, agent_uuid, and company_name into req.user.
    5. Updated src/lib/admin-api.ts (startViewAsUser) and src/app/(dashboard)/user-accounts/page.tsx to pass user.id and target user details to backend.
    6. Verified SidebarProfile.tsx, useInactivityTimeout.ts, and login/page.tsx continue to wipe localStorage.removeItem('backone_view_as') on sign out or login.
    7. Ran node test/view_as_full_impersonation_test.js -> GREEN pass achieved.
    8. Ran npx tsc --noEmit -> 0 TypeScript compilation errors.
    9. Ran npm run build -> Clean production build.
  • Outcome: View-As mode now provides 100% full impersonation of target user accounts while preserving audit trails and guaranteeing instant session cleanup on logout.