From 9137eb02b0dc72a70bc502b70a37373fa77e91d4 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 14 Oct 2024 12:20:46 -0300 Subject: [PATCH] cookbook: reacting to auth events. --- docs/cookbook/auth.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/cookbook/auth.md b/docs/cookbook/auth.md index 2e64c30..8f53bd7 100644 --- a/docs/cookbook/auth.md +++ b/docs/cookbook/auth.md @@ -60,3 +60,26 @@ relay.RejectFilter = append(relay.RejectFilter, func(ctx context.Context, filter } }) ``` + +## Reacting to a successful authentication + +Each `khatru.WebSocket` object has an `.Authed` channel that is closed whenever that connection performs a successful authentication. + +You can use that to emulate a listener for these events in case you want to keep track of who is authenticating in real time and not only check it when they request for something. + +```go + relay.OnConnect = append(relay.OnConnect, + khatru.RequestAuth, + func(ctx context.Context) { + go func(ctx context.Context) { + conn := khatru.GetConnection(ctx) + select { + case <-ctx.Done(): + fmt.Println("connection closed") + case <-conn.Authed: + fmt.Println("authenticated as", conn.AuthedPublicKey) + } + }(ctx) + }, + ) +```