fix: login failures on Postgres when IP is null (#737)

This commit is contained in:
Alessandro (Ale) Segala
2025-07-09 06:45:07 -07:00
committed by GitHub
parent 45f42772b1
commit e1de593dcd
3 changed files with 11 additions and 8 deletions

View File

@@ -29,7 +29,7 @@ func (m *RateLimitMiddleware) Add(limit rate.Limit, burst int) gin.HandlerFunc {
// Skip rate limiting for localhost and test environment // Skip rate limiting for localhost and test environment
// If the client ip is localhost the request comes from the frontend // If the client ip is localhost the request comes from the frontend
if ip == "127.0.0.1" || ip == "::1" || common.EnvConfig.AppEnv == "test" { if ip == "" || ip == "127.0.0.1" || ip == "::1" || common.EnvConfig.AppEnv == "test" {
c.Next() c.Next()
return return
} }

View File

@@ -70,12 +70,17 @@ func (s *AuditLogService) CreateNewSignInWithEmail(ctx context.Context, ipAddres
// Count the number of times the user has logged in from the same device // Count the number of times the user has logged in from the same device
var count int64 var count int64
err := tx. stmt := tx.
WithContext(ctx). WithContext(ctx).
Model(&model.AuditLog{}). Model(&model.AuditLog{}).
Where("user_id = ? AND ip_address = ? AND user_agent = ?", userID, ipAddress, userAgent). Where("user_id = ? AND user_agent = ?", userID, ipAddress)
Count(&count). if ipAddress == "" {
Error // An empty IP address is stored as NULL in the database
stmt = stmt.Where("ip_address IS NULL")
} else {
stmt = stmt.Where("ip_address = ?", ipAddress)
}
err := stmt.Count(&count).Error
if err != nil { if err != nil {
log.Printf("Failed to count audit logs: %v", err) log.Printf("Failed to count audit logs: %v", err)
return createdAuditLog return createdAuditLog

View File

@@ -469,9 +469,7 @@ func (s *UserService) ExchangeOneTimeAccessToken(ctx context.Context, token stri
return model.User{}, "", err return model.User{}, "", err
} }
if ipAddress != "" && userAgent != "" {
s.auditLogService.Create(ctx, model.AuditLogEventOneTimeAccessTokenSignIn, ipAddress, userAgent, oneTimeAccessToken.User.ID, model.AuditLogData{}, tx) s.auditLogService.Create(ctx, model.AuditLogEventOneTimeAccessTokenSignIn, ipAddress, userAgent, oneTimeAccessToken.User.ID, model.AuditLogData{}, tx)
}
err = tx.Commit().Error err = tx.Commit().Error
if err != nil { if err != nil {