# Remove Invite-by-Email Implementation Plan > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. **Goal:** Remove the "Invite by email" mode from the admin user management panel, leaving only the direct-add (admin-set password) flow. **Architecture:** Strip the mode toggle from the `UserManager` component and remove the invite branch from the POST handler in `app/api/admin/users/route.ts`. Delete the now-unreachable `app/auth/invite-callback/` page. No DB changes required. **Tech Stack:** Next.js App Router, TypeScript, Vitest ## Global Constraints - Never commit `.env` or API keys - Every DB mutation writes to `audit_log` via `write_audit_log` RPC - `basePath` is `/ims` — all internal links must carry this prefix - Run `npm run build` and `npm test` before every commit; both must pass --- ### Task 1: Strip invite mode from UI component and API route **Files:** - Modify: `components/admin/user-manager.tsx` - Modify: `app/api/admin/users/route.ts` **Interfaces:** - Consumes: nothing from other tasks - Produces: `UserManager` with no mode toggle; POST `/api/admin/users` always requires `password` - [ ] **Step 1: Update `components/admin/user-manager.tsx`** Replace the entire file content: ```tsx 'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { ALL_ROLES, type UserRole } from '@/lib/auth/roles' export type AdminUser = { id: string name: string email: string role: string department: string | null site_id: string | null active: boolean } export type SiteOption = { id: string; name: string } interface Props { users: AdminUser[] sites: SiteOption[] } const EMPTY_FORM = { email: '', name: '', phone: '', role: 'reporter' as UserRole, site_id: '', password: '' } export function UserManager({ users, sites }: Props) { const router = useRouter() const [busyId, setBusyId] = useState(null) const [error, setError] = useState(null) const [form, setForm] = useState(EMPTY_FORM) const [submitting, setSubmitting] = useState(false) const [confirmDeleteId, setConfirmDeleteId] = useState(null) const patchUser = async (id: string, update: Record) => { setBusyId(id) setError(null) const res = await fetch('/ims/api/admin/users', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id, ...update }), }) setBusyId(null) if (!res.ok) { const data = await res.json().catch(() => ({})) setError(data.error ?? 'Update failed') return } router.refresh() } const deleteUser = async (id: string) => { setBusyId(id) setError(null) const res = await fetch(`/ims/api/admin/users?id=${encodeURIComponent(id)}`, { method: 'DELETE' }) setBusyId(null) setConfirmDeleteId(null) if (!res.ok) { const data = await res.json().catch(() => ({})) setError(data.error ?? 'Delete failed') return } router.refresh() } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setSubmitting(true) setError(null) const res = await fetch('/ims/api/admin/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: form.email, name: form.name || undefined, phone: form.phone || undefined, role: form.role, site_id: form.site_id || undefined, password: form.password, }), }) setSubmitting(false) if (!res.ok) { const data = await res.json().catch(() => ({})) setError(data.error ?? 'Add failed') return } setForm(EMPTY_FORM) router.refresh() } return (

Users

setForm(v => ({ ...v, email: e.target.value }))} className="border border-gray-300 rounded-lg px-3 py-2 text-sm w-52" /> setForm(v => ({ ...v, name: e.target.value }))} className="border border-gray-300 rounded-lg px-3 py-2 text-sm w-40" /> setForm(v => ({ ...v, phone: e.target.value }))} className="border border-gray-300 rounded-lg px-3 py-2 text-sm w-44" /> setForm(v => ({ ...v, password: e.target.value }))} className="border border-gray-300 rounded-lg px-3 py-2 text-sm w-48" />
{error &&

{error}

}
{users.map(u => ( ))}
Name Email Role Site Status
{u.name || '—'} {u.email} {confirmDeleteId === u.id ? ( Sure? ) : ( )}
) } ``` - [ ] **Step 2: Update `app/api/admin/users/route.ts` POST handler** Replace only the `POST` function (lines 21–104) with: ```ts export async function POST(request: NextRequest) { const { supabase, user } = await requireAdmin() if (!user) return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) const body: { email?: string; name?: string; phone?: string; role?: string; site_id?: string; password?: string } = await request.json().catch(() => ({})) const email = (body.email ?? '').trim().toLowerCase() if (!email || !email.includes('@')) return NextResponse.json({ error: 'Valid email required' }, { status: 422 }) if (body.role && !isValidRole(body.role)) return NextResponse.json({ error: 'Invalid role' }, { status: 422 }) const password = (body.password ?? '').trim() if (!password) return NextResponse.json({ error: 'Password required' }, { status: 422 }) if (password.length < 8) return NextResponse.json({ error: 'Password must be at least 8 characters' }, { status: 422 }) let admin try { admin = createAdminClient() } catch { return NextResponse.json( { error: 'User creation unavailable: SUPABASE_SERVICE_ROLE_KEY not configured' }, { status: 503 }, ) } const { data: created, error: createError } = await admin.auth.admin.createUser({ email, password, email_confirm: true, user_metadata: { full_name: body.name ?? '' }, }) if (createError || !created?.user) return NextResponse.json( { error: createError?.message ?? 'User creation failed' }, { status: (createError as { status?: number } | null)?.status ?? 500 }, ) const newUserId = created.user.id const { error: profileError } = await admin .from('users') .update({ name: body.name ?? '', phone: (body.phone ?? '').trim() || null, role: body.role ?? 'reporter', site_id: body.site_id ?? null, }) .eq('id', newUserId) if (profileError) return NextResponse.json( { error: 'User created but profile update failed' }, { status: 500 }, ) await supabase.rpc('write_audit_log', { p_table_name: 'users', p_record_id: newUserId, p_action: 'created', p_new_value: { email, role: body.role ?? 'reporter', site_id: body.site_id ?? null }, }) return NextResponse.json({ id: newUserId }, { status: 201 }) } ``` - [ ] **Step 3: Run tests and build** ```bash npm test npm run build ``` Expected: all tests pass, build succeeds with no TypeScript errors. - [ ] **Step 4: Commit** ```bash git add components/admin/user-manager.tsx app/api/admin/users/route.ts git commit -m "feat(admin): remove invite-by-email, direct-add only" ``` --- ### Task 2: Delete invite-callback page **Files:** - Delete: `app/auth/invite-callback/page.tsx` - Delete: `app/auth/invite-callback/` (directory) **Interfaces:** - Consumes: nothing - Produces: route `/auth/invite-callback` no longer exists - [ ] **Step 1: Delete the callback directory** ```bash rm -rf app/auth/invite-callback ``` - [ ] **Step 2: Verify `app/auth/` is now empty (or remove it too)** ```bash ls app/auth/ ``` If the directory is empty, remove it: ```bash rmdir app/auth ``` - [ ] **Step 3: Run build to confirm no dangling imports** ```bash npm run build ``` Expected: build succeeds. No file anywhere imports from `app/auth/invite-callback`. - [ ] **Step 4: Commit** ```bash git add -A git commit -m "chore: delete invite-callback page" ```