Skip to content

Commit

Permalink
Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
aunefyren committed Mar 28, 2024
1 parent a65eb7a commit a174c3e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
8 changes: 5 additions & 3 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ func RegisterUser(context *gin.Context) {

randomString := randstr.String(8)
user.VerificationCode = strings.ToUpper(randomString)
user.VerificationCodeExpiration = time.Now().Add(time.Hour * 24 * 2)
timeExpir := time.Now().Add(time.Hour * 24 * 2)
user.VerificationCodeExpiration = &timeExpir

randomString = randstr.String(8)
user.ResetCode = strings.ToUpper(randomString)
user.ResetExpiration = time.Now()
timeResetExp := time.Now()
user.ResetExpiration = &timeResetExp

// Get configuration
config, err := config.GetConfig()
Expand Down Expand Up @@ -248,7 +250,7 @@ func VerifyUser(context *gin.Context) {
context.JSON(http.StatusBadRequest, gin.H{"error": "Verification code invalid."})
context.Abort()
return
} else if time.Now().After(expiration) {
} else if expiration == nil || time.Now().After(*expiration) {
context.JSON(http.StatusBadRequest, gin.H{"error": "Verification code has expired, request a new one."})
context.Abort()
return
Expand Down
11 changes: 7 additions & 4 deletions database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,19 @@ func VerifyUserHasVerificationCode(userID uuid.UUID) (bool, error) {
}

// Verify if user has a verification code set
func VerifyUserVerificationCodeMatches(userID uuid.UUID, verificationCode string) (bool, time.Time, error) {
func VerifyUserVerificationCodeMatches(userID uuid.UUID, verificationCode string) (bool, *time.Time, error) {

var user models.User
var now = time.Now()

userrecords := Instance.Where("`users`.enabled = ?", 1).Where("`users`.ID = ?", userID).Where("`users`.verification_code = ?", verificationCode).Find(&user)

if userrecords.Error != nil {
return false, time.Now(), userrecords.Error
return false, &now, userrecords.Error
}

if userrecords.RowsAffected != 1 {
return false, time.Now(), nil
return false, &now, nil
} else {
return true, user.VerificationCodeExpiration, nil
}
Expand Down Expand Up @@ -379,7 +380,9 @@ func CensorUserObject(user models.User) models.User {
user.Email = "REDACTED"
user.VerificationCode = "REDACTED"
user.ResetCode = "REDACTED"
user.ResetExpiration = time.Now()
now := time.Now()
user.ResetExpiration = &now
user.VerificationCodeExpiration = &now
user.SundayAlert = false
user.StravaCode = nil

Expand Down
4 changes: 2 additions & 2 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type User struct {
Enabled bool `json:"enabled" gorm:"not null; default: false"`
Verified bool `json:"verified" gorm:"not null; default: false"`
VerificationCode string `json:"verification_code"`
VerificationCodeExpiration time.Time `json:"verification_code_expiration"`
VerificationCodeExpiration *time.Time `json:"verification_code_expiration"`
ResetCode string `json:"reset_code"`
ResetExpiration time.Time `json:"reset_expiration"`
ResetExpiration *time.Time `json:"reset_expiration"`
SundayAlert bool `json:"sunday_alert" gorm:"not null; default: false"`
BirthDate *time.Time `json:"birth_date" gorm:"default: null"`
StravaCode *string `json:"strava_code" gorm:"default: null"`
Expand Down

0 comments on commit a174c3e

Please sign in to comment.