🐛 Trying to fix nats: subject does not match consumer

This commit is contained in:
LittleSheep 2025-01-27 16:12:09 +08:00
parent 144219f0aa
commit e4ba72725e
2 changed files with 4 additions and 2 deletions

View File

@ -72,7 +72,7 @@ func InitializeToNexus() error {
Name: "OTPs", Name: "OTPs",
Subjects: []string{FactorOtpPrefix + ">"}, Subjects: []string{FactorOtpPrefix + ">"},
Storage: nats.MemoryStorage, Storage: nats.MemoryStorage,
MaxAge: 5 * time.Minute, MaxAge: 30 * time.Minute,
} }
_, err = Jt.AddStream(jetstreamCfg) _, err = Jt.AddStream(jetstreamCfg)
if err != nil && !errors.Is(err, nats.ErrStreamNameAlreadyInUse) { if err != nil && !errors.Is(err, nats.ErrStreamNameAlreadyInUse) {

View File

@ -119,11 +119,13 @@ func CheckFactor(factor models.AuthFactor, code string) error {
) )
case models.EmailPasswordFactor: case models.EmailPasswordFactor:
identifier := fmt.Sprintf("%s%d", gap.FactorOtpPrefix, factor.ID) identifier := fmt.Sprintf("%s%d", gap.FactorOtpPrefix, factor.ID)
sub, err := gap.Jt.PullSubscribe(identifier, "otp_validator") sub, err := gap.Jt.PullSubscribe(identifier, "otp_validator", nats.BindStream("OTPs"))
if err != nil { if err != nil {
log.Error().Err(err).Msg("Error subscribing to subject when validating factor code...") log.Error().Err(err).Msg("Error subscribing to subject when validating factor code...")
return fmt.Errorf("error subscribing to subject: %v", err) return fmt.Errorf("error subscribing to subject: %v", err)
} }
// ChatGPT said the subscription will be reused, so we don't need to unsubscribe
// defer sub.Unsubscribe()
msgs, err := sub.Fetch(1, nats.MaxWait(3*time.Second)) msgs, err := sub.Fetch(1, nats.MaxWait(3*time.Second))
if err != nil { if err != nil {