Skip to content

Commit

Permalink
resend verification code to verify updated email
Browse files Browse the repository at this point in the history
  • Loading branch information
pilinux committed Sep 16, 2023
1 parent 5362875 commit 4541e8b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ _Note:_ For **MySQL** driver, please [check issue: 7][42]
| handler | logout.go | `1016` |
| handler | passwordReset.go | `1021 - 1030` |
| handler | twoFA.go | `1051 - 1056` |
| handler | verification.go | `1061 - 1064` |
| handler | verification.go | `1061 - 1065` |
| service | common.go | `401 - 406` |
| service | security.go | `501` |

Expand Down
43 changes: 43 additions & 0 deletions controller/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,46 @@ func GetUnverifiedEmail(c *gin.Context) {

renderer.Render(c, resp, statusCode)
}

// ResendVerificationCodeToModifyActiveEmail issues new verification code upon request
//
// dependency: email service, email verification service, Redis,
// relational database, JWT
func ResendVerificationCodeToModifyActiveEmail(c *gin.Context) {
// verify that email service is enabled in .env
if !config.IsEmailService() {
renderer.Render(c, gin.H{"message": "email service not enabled"}, http.StatusNotImplemented)
return
}

// verify that email verification service is enabled in .env
if !config.IsEmailVerificationService() {
renderer.Render(c, gin.H{"message": "email verification service not enabled"}, http.StatusNotImplemented)
return
}

// verify that Redis is enabled in .env
if !config.IsRedis() {
renderer.Render(c, gin.H{"message": "Redis not enabled"}, http.StatusNotImplemented)
return
}

// verify that RDBMS is enabled in .env
if !config.IsRDBMS() {
renderer.Render(c, gin.H{"message": "relational database not enabled"}, http.StatusNotImplemented)
return
}

// verify that JWT service is enabled in .env
if !config.IsJWT() {
renderer.Render(c, gin.H{"message": "JWT service not enabled"}, http.StatusNotImplemented)
return
}

// get claims
claims := service.GetClaims(c)

resp, statusCode := handler.ResendVerificationCodeToModifyActiveEmail(claims)

renderer.Render(c, resp, statusCode)
}
2 changes: 2 additions & 0 deletions example/router/setupRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ func SetupRouter(configure *gconfig.Configuration) (*gin.Engine, error) {
rEmail.POST("update", gcontroller.UpdateEmail)
// retrieve the email which needs to be verified
rEmail.GET("unverified", gcontroller.GetUnverifiedEmail)
// resend verification code to verify the modified email address
rEmail.POST("resend-verification-email", gcontroller.ResendVerificationCodeToModifyActiveEmail)

// User
rUsers := v1.Group("users")
Expand Down
73 changes: 73 additions & 0 deletions handler/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,76 @@ func GetUnverifiedEmail(claims middleware.MyCustomClaims) (httpResponse model.HT
httpStatusCode = http.StatusOK
return
}

// ResendVerificationCodeToModifyActiveEmail receives tasks from controller.ResendVerificationCodeToModifyActiveEmail
func ResendVerificationCodeToModifyActiveEmail(claims middleware.MyCustomClaims) (httpResponse model.HTTPResponse, httpStatusCode int) {
// check auth validity
ok := service.ValidateAuthID(claims.AuthID)
if !ok {
httpResponse.Message = "validation failed - access denied"
httpStatusCode = http.StatusUnauthorized
return
}

// read DB
db := database.GetDB()
tempEmail := model.TempEmail{}

// check 'temp_emails'
err := db.Where("id_auth = ?", claims.AuthID).First(&tempEmail).Error
if err != nil {
if err.Error() != database.RecordNotFound {
// db read error
log.WithError(err).Error("error code: 1065.1")
httpResponse.Message = "internal server error"
httpStatusCode = http.StatusInternalServerError
return
}

httpResponse.Message = "no pending request"
httpStatusCode = http.StatusBadRequest
return
}

// verification is pending to modify current email
if err == nil {
// decipher
if tempEmail.Email == "" {
if !config.IsCipher() {
e := errors.New("check env: ACTIVATE_CIPHER")
log.WithError(e).Error("error code: 1065.2")
httpResponse.Message = "internal server error"
httpStatusCode = http.StatusInternalServerError
return
}

tempEmail.Email, err = service.DecryptEmail(tempEmail.EmailNonce, tempEmail.EmailCipher)
if err != nil {
log.WithError(err).Error("error code: 1065.3")
httpResponse.Message = "internal server error"
httpStatusCode = http.StatusInternalServerError
return
}
}
}

// issue new verification code
emailDelivered, err := service.SendEmail(tempEmail.Email, model.EmailTypeVerifyUpdatedEmail)
if err != nil {
log.WithError(err).Error("error code: 1065.4")
httpResponse.Message = "email delivery service failed"
httpStatusCode = http.StatusInternalServerError
return
}
if err == nil {
if !emailDelivered {
httpResponse.Message = "failed to send verification email"
httpStatusCode = http.StatusServiceUnavailable
return
}
}

httpResponse.Message = "sent verification email"
httpStatusCode = http.StatusOK
return
}

0 comments on commit 4541e8b

Please sign in to comment.