Skip to content

Commit

Permalink
fixed minor issues (#451)
Browse files Browse the repository at this point in the history
Co-authored-by: Garrett Ladley <[email protected]>
  • Loading branch information
DOOduneye and garrettladley authored Apr 2, 2024
1 parent c0c3bcb commit ac1a65b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 88 deletions.
35 changes: 22 additions & 13 deletions backend/src/services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/GenerateNU/sac/backend/src/utilities"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt"
"gorm.io/gorm"
)

type AuthServiceInterface interface {
Expand Down Expand Up @@ -272,34 +273,42 @@ func (a *AuthService) SendCode(email string) *errors.Error {
return nil
}

/* trunk-ignore(golangci-lint/cyclop) */
func (a *AuthService) VerifyEmail(emailBody models.VerifyEmailRequestBody) *errors.Error {
if err := a.Validate.Struct(emailBody); err != nil {
return &errors.FailedToValidateUser
}

user, err := transactions.GetUserByEmail(a.DB, emailBody.Email)
if err != nil {
return err
}

func verifyEmailHelper(user *models.User, token string, db *gorm.DB) *errors.Error {
if user.IsVerified {
return &errors.EmailAlreadyVerified
}

otp, otpErr := transactions.GetToken(a.DB, emailBody.Token, models.EmailVerificationType)
otp, otpErr := transactions.GetToken(db, token, models.EmailVerificationType)
if otpErr != nil {
return otpErr
}

if otp.Token != emailBody.Token {
if otp.Token != token {
return &errors.InvalidOTP
}

if otp.ExpiresAt.Before(time.Now().UTC()) {
return &errors.OTPExpired
}

return nil
}

func (a *AuthService) VerifyEmail(emailBody models.VerifyEmailRequestBody) *errors.Error {
if err := a.Validate.Struct(emailBody); err != nil {
return &errors.FailedToValidateUser
}

user, err := transactions.GetUserByEmail(a.DB, emailBody.Email)
if err != nil {
return err
}

verifyErr := verifyEmailHelper(user, emailBody.Token, a.DB)
if verifyErr != nil {
return verifyErr
}

tx := a.DB.Begin()
defer func() {
if r := recover(); r != nil {
Expand Down
73 changes: 0 additions & 73 deletions backend/src/transactions/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,6 @@ import (
"gorm.io/gorm"
)

// func SaveOTP(db *gorm.DB, userID uuid.UUID, otp string) *errors.Error {
// otpModel := models.Verification{
// UserID: userID,
// Token: otp,
// ExpiresAt: time.Now().Add(time.Minute * 30).UTC(),
// Type: models.EmailVerificationType,
// }

// if err := db.Create(&otpModel).Error; err != nil {
// return &errors.FailedToSaveOTP
// }

// return nil
// }

// // @Deprecated
// func GetOTP(db *gorm.DB, userID uuid.UUID) (*models.Verification, *errors.Error) {
// var otp models.Verification
// if err := db.Where("user_id = ? AND type = ?", userID, models.EmailVerificationType).First(&otp).Error; err != nil {
// if stdliberrors.Is(err, gorm.ErrRecordNotFound) {
// return nil, &errors.OTPNotFound
// }

// return nil, &errors.FailedToGetOTP
// }

// return &otp, nil
// }

// func DeleteOTP(db *gorm.DB, userID uuid.UUID) *errors.Error {
// if err := db.Where("user_id = ? AND type = ?", userID, models.EmailVerificationType).Delete(&models.Verification{}).Error; err != nil {
// return &errors.FailedToDeleteOTP
// }

// return nil
// }

// func SavePasswordResetToken(db *gorm.DB, userID uuid.UUID, token string) *errors.Error {
// passwordReset := models.Verification{
// UserID: userID,
// Token: token,
// ExpiresAt: time.Now().Add(time.Hour * 24).UTC(),
// Type: models.PasswordResetType,
// }

// if err := db.Create(&passwordReset).Error; err != nil {
// return &errors.FailedToCreatePasswordReset
// }

// return nil
// }

// func DeletePasswordResetToken(db *gorm.DB, token string) *errors.Error {
// if err := db.Where("token = ? AND type = ?", token, models.PasswordResetType).Delete(&models.Verification{}).Error; err != nil {
// return &errors.FailedToDeletePasswordReset
// }

// return nil
// }

// // @Deprecated
// func GetPasswordResetToken(db *gorm.DB, token string) (*models.Verification, *errors.Error) {
// passwordReset := models.Verification{}
// if err := db.Where("token = ? AND type = ?", token, models.PasswordResetType).First(&passwordReset).Error; err != nil {
// if err == gorm.ErrRecordNotFound {
// return nil, &errors.PasswordResetTokenNotFound
// }
// return nil, &errors.FailedToGetPasswordResetToken
// }

// return &passwordReset, nil
// }

func GetToken(db *gorm.DB, token string, tokenType models.VerificationType) (*models.Verification, *errors.Error) {
tokenModel := models.Verification{}
if err := db.Where("token = ? AND type = ?", token, tokenType).First(&tokenModel).Error; err != nil {
Expand Down
4 changes: 2 additions & 2 deletions config/local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ superuser:
password: Password#!1
auth:
accesskey: g(r|##*?>\Qp}h37e+,T2
accesstokenexpiry: 30 # in minutes
accesstokenexpiry: 60 # in minutes
refreshkey: amk*2!gG}1i"8D9RwJS$p
refreshtokenexpiry: 60 # in days
refreshtokenexpiry: 30 # in days

0 comments on commit ac1a65b

Please sign in to comment.