Connect.js mounts an iframe and reads browser globals. Initialize the widget in a Client Component, then exchange the auth code in server-only code.
Client component
The dynamic import keeps package loading inside the browser lifecycle. NEXT_PUBLIC_MONO_PUBLIC_KEY is intentionally public.
Route handler
The Route Handler exchanges the short-lived code with the secret key. It must also authenticate the application user and store the returned account ID against that user.
Never name the secret NEXT_PUBLIC_MONO_SECRET_KEY. Variables with the NEXT_PUBLIC_ prefix are included in client bundles.
Reauthorization
Inside the Client Component, initialize with the existing account ID instead of calling setup().
See Reauthorization for the complete flow.
Next.js-specific checks
The component containing Connect.js begins with "use client".
Widget creation runs inside useEffect.
The public key uses NEXT_PUBLIC_; the secret key never does.
The Route Handler authenticates the application user before storing an account ID.
Server errors returned to the browser do not contain the secret key or raw upstream response.