When a relay returns a CLOSED message with an auth-required: prefix, you must authenticate.
Using SimplePool (Recommended):
Pass an onauth handler to subscribeMany, subscribeManyEose, or subscribeEose. The pool will automatically sign the challenge and resubscribe.
Using Relay directly:
If using the Relay class, you must manually handle the onclose event, call relay.auth(callback), and restart the subscription.
// SimplePool approach
import { SimplePool } from '@nostr/tools/pool'
const pool = new SimplePool()
pool.subscribeMany(
['wss://myrelay.com'],
[{ '#t': ['restricted'] }],
{
onevent(event) { console.log('got event:', event) },
onauth: (eventTemplate) => window.nostr.signEvent(eventTemplate),
}
)