Skip to content

Commit

Permalink
feat: add endpoint resend verif email
Browse files Browse the repository at this point in the history
  • Loading branch information
reynaldineo committed Mar 18, 2024
1 parent 7a29184 commit f238880
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 21 deletions.
24 changes: 23 additions & 1 deletion controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type (
Me(ctx *gin.Context)
GetAllPagination(ctx *gin.Context)
Verify(ctx *gin.Context)
ResendVerifyEmail(ctx *gin.Context)
}

userController struct {
Expand All @@ -34,9 +35,30 @@ func NewUserController(us service.UserService, jwt service.JWTService) UserContr
}
}

func (c *userController) ResendVerifyEmail(ctx *gin.Context) {
var email dto.UserResendVerifyEmailRequest

if err := ctx.ShouldBind(&email); err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
ctx.JSON(http.StatusBadRequest, res)
return
}

err := c.userService.ResendVerifyEmail(ctx.Request.Context(), email.Email)
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_RESEND_VERIFY_EMAIL, err.Error(), nil)
ctx.JSON(http.StatusBadRequest, res)
return
}

res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_RESEND_VERIFY_EMAIL, nil)
res.Message = "Email has been sent"
ctx.JSON(http.StatusOK, res.Message)
}

func (c *userController) Verify(ctx *gin.Context) {
token := ctx.Query("token")

_, err := c.jwtService.ValidateToken(token)
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_VERIFY_USER, err.Error(), nil)
Expand Down
51 changes: 32 additions & 19 deletions dto/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,35 @@ import (

const (
// Failed
MESSAGE_FAILED_REGISTER_USER = "failed create user"
MESSAGE_FAILED_GET_USER = "failed get user"
MESSAGE_FAILED_LOGIN = "failed login"
MESSAGE_FAILED_UPDATE_USER = "failed update user"
MESSAGE_FAILED_VERIFY_USER = "failed verify user"
MESSAGE_FAILED_REGISTER_USER = "failed create user"
MESSAGE_FAILED_GET_USER = "failed get user"
MESSAGE_FAILED_LOGIN = "failed login"
MESSAGE_FAILED_UPDATE_USER = "failed update user"
MESSAGE_FAILED_VERIFY_USER = "failed verify user"
MESSAGE_FAILED_RESEND_VERIFY_EMAIL = "failed resend verify email"

// Success
MESSAGE_SUCCESS_REGISTER_USER = "success create user"
MESSAGE_SUCCESS_GET_USER = "success get user"
MESSAGE_SUCCESS_LOGIN = "success login"
MESSAGE_SUCCESS_UPDATE_USER = "success update user"
MESSAGE_SUCCESS_VERIFY_USER = "success verify user"
MESSAGE_SUCCESS_REGISTER_USER = "success create user"
MESSAGE_SUCCESS_GET_USER = "success get user"
MESSAGE_SUCCESS_LOGIN = "success login"
MESSAGE_SUCCESS_UPDATE_USER = "success update user"
MESSAGE_SUCCESS_VERIFY_USER = "success verify user"
MESSAGE_SUCCESS_RESEND_VERIFY_EMAIL = "success resend verify email"
)

var (
ErrRoleNotAllowed = errors.New("denied access for \"%v\" role")
ErrCreateUser = errors.New("failed to create user")
ErrGetUserById = errors.New("failed to get user by id")
ErrEmailAlreadyExists = errors.New("email already exist")
ErrUpdateUser = errors.New("failed to update user")
ErrUserNotFound = errors.New("user not found")
ErrCredentialsNotMatched = errors.New("credentials not matched")
ErrAccountNotVerified = errors.New("account not verified")
ErrEmailFormatInvalid = errors.New("email format invalid")
ErrRoleNotAllowed = errors.New("denied access for \"%v\" role")
ErrCreateUser = errors.New("failed to create user")
ErrGetUserById = errors.New("failed to get user by id")
ErrEmailAlreadyExists = errors.New("email already exist")
ErrUpdateUser = errors.New("failed to update user")
ErrUserNotFound = errors.New("user not found")
ErrCredentialsNotMatched = errors.New("credentials not matched")
ErrAccountNotVerified = errors.New("account not verified")
ErrEmailFormatInvalid = errors.New("email format invalid")
ErrAccountAlreadyVerified = errors.New("account already verified")
ErrGenerateVerificationEmail = errors.New("failed to generate verification email")
ErrSendEmail = errors.New("failed to send email")
)

type (
Expand Down Expand Up @@ -57,4 +62,12 @@ type (
Data []UserResponse `json:"data"`
PaginationMetadata
}

UserResendVerifyEmailRequest struct {
Email string `json:"email" form:"email" binding:"required"`
}

UserResendVerifyEmailResponse struct {
Email string `json:"email"`
}
)
3 changes: 2 additions & 1 deletion routes/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func User(route *gin.Engine, userController controller.UserController, jwtServic
routes.PATCH("", middleware.Authenticate(jwtService), userController.Update)
routes.GET("/me", middleware.Authenticate(jwtService), userController.Me)
routes.GET("", middleware.Authenticate(jwtService), middleware.OnlyAllow(constants.ENUM_ROLE_ADMIN), userController.GetAllPagination)
routes.GET("/verify", userController.Verify )
routes.GET("/verify", userController.Verify)
routes.POST("/verify/resend", userController.ResendVerifyEmail)
}
}
24 changes: 24 additions & 0 deletions service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
GetAllPagination(ctx context.Context, req dto.PaginationQuery) (dto.UserPaginationResponse, error)

generateVerificationEmail(userEmail string) (utils.Email, error)
ResendVerifyEmail(ctx context.Context, email string) error
}

userService struct {
Expand All @@ -37,6 +38,29 @@ func NewUserService(ur repository.UserRepository) UserService {
}
}

func (s *userService) ResendVerifyEmail(ctx context.Context, email string) error {
user, err := s.userRepo.GetUserByEmail(email)
if err != nil {
return dto.ErrUserNotFound
}

if user.Verified {
return dto.ErrAccountAlreadyVerified
}

emailData, err := s.generateVerificationEmail(email)
if err != nil {
return dto.ErrGenerateVerificationEmail
}

err = utils.SendMail(emailData)
if err != nil {
return dto.ErrSendEmail
}

return nil
}

func (s *userService) RegisterUser(ctx context.Context, req dto.UserRequest) (dto.UserResponse, error) {
email, _ := s.userRepo.CheckEmailExist(req.Email)
if email {
Expand Down

0 comments on commit f238880

Please sign in to comment.