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/attemptedAuthStatus.Requested- Relay requested authenticationAuthStatus.PendingSignature- Waiting for user to sign auth eventAuthStatus.DeniedSignature- User denied signingAuthStatus.PendingResponse- Waiting for relay responseAuthStatus.Forbidden- Authentication failedAuthStatus.Ok- Authentication successful
Methods:
doAuth(sign)- Signs and sends a response to the current challengeauthenticate(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 anAuthResult({ok, status, reason}) once the relay accepts or refuses, or it never challenges, orsignalaborts.okis true when the socket is open and the relay either accepted or never askedcleanup()- 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)
}