diff --git a/v4/events/natsjs/nats.go b/v4/events/natsjs/nats.go index 40f0179d..3deadfde 100644 --- a/v4/events/natsjs/nats.go +++ b/v4/events/natsjs/nats.go @@ -69,6 +69,11 @@ func connectToNatsJetStream(options Options) (nats.JetStreamContext, error) { nopts.Name = options.Name } + if options.Username != "" && options.Password != "" { + nopts.User = options.Username + nopts.Password = options.Password + } + conn, err := nopts.Connect() if err != nil { tls := nopts.TLSConfig != nil diff --git a/v4/events/natsjs/options.go b/v4/events/natsjs/options.go index 2ace150b..f7e83d5f 100644 --- a/v4/events/natsjs/options.go +++ b/v4/events/natsjs/options.go @@ -17,6 +17,8 @@ type Options struct { SyncPublish bool Name string DisableDurableStreams bool + Username string + Password string } // Option is a function which configures options. @@ -84,3 +86,11 @@ func DisableDurableStreams() Option { o.DisableDurableStreams = true } } + +// Authenticate authenticates the connection with the given username and password. +func Authenticate(username, password string) Option { + return func(o *Options) { + o.Username = username + o.Password = password + } +}