Skip to content

Auth ​

AuthState tracks the NIP-42 authentication flow for one socket. Every socket carries one as socket.auth.

AuthState ​

Status Values:

  • AuthStatus.None - No authentication required/attempted
  • AuthStatus.Requested - Relay requested authentication
  • AuthStatus.PendingSignature - Waiting for user to sign auth event
  • AuthStatus.DeniedSignature - User denied signing
  • AuthStatus.PendingResponse - Waiting for relay response
  • AuthStatus.Forbidden - Authentication failed
  • AuthStatus.Ok - Authentication successful

Methods:

  • doAuth(sign) - Signs and sends a response to the current challenge
  • authenticate(sign, signal) - Opens the socket, waits up to three seconds for a challenge, and answers it, answering again if an earlier answer was refused or the signer failed. Resolves to an AuthResult ({ok, status, reason}) once the relay accepts or refuses, or it never challenges, or signal aborts. ok is true when the socket is open and the relay either accepted or never asked
  • cleanup() - Removes event listeners

Events:

  • AuthStateEvent.Status - Emitted when authentication status changes

Example ​

typescript
import {AuthStateEvent} from '@welshman/net'

socket.auth.on(AuthStateEvent.Status, (status) => {
  console.log('Auth status:', status)
})

const result = await socket.auth.authenticate(
  template => signer.sign(template),
  AbortSignal.timeout(30_000),
)

if (!result.ok) {
  console.log('Auth failed:', result.status, result.reason)
}