Skip to content

Commit

Permalink
feature: resend otp
Browse files Browse the repository at this point in the history
feature: resend otp
  • Loading branch information
masnann authored Nov 9, 2023
2 parents 66d6edd + 39145ca commit 8191c3b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/labstack/echo/v4 v4.11.2
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/wneessen/go-mail v0.4.0
golang.org/x/crypto v0.14.0
gorm.io/driver/mysql v1.5.2
gorm.io/gorm v1.25.5
Expand All @@ -30,7 +31,6 @@ require (
github.com/stretchr/objx v0.5.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/wneessen/go-mail v0.4.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions module/auth/domain/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ type EmailRequest struct {
Email string `form:"email" json:"email" validate:"required"`
OTP string `form:"otp" json:"otp" validate:"required"`
}

type ResendOTPRequest struct {
Email string `form:"email" json:"email" validate:"required"`
}
32 changes: 31 additions & 1 deletion module/auth/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package handler

import (
"net/http"

"github.com/capstone-kelompok-7/backend-disappear/module/auth"
"github.com/capstone-kelompok-7/backend-disappear/module/auth/domain"
"github.com/capstone-kelompok-7/backend-disappear/module/users"
user "github.com/capstone-kelompok-7/backend-disappear/module/users/domain"
"github.com/capstone-kelompok-7/backend-disappear/utils"
"github.com/capstone-kelompok-7/backend-disappear/utils/email"
"github.com/capstone-kelompok-7/backend-disappear/utils/response"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"net/http"
)

type AuthHandler struct {
Expand Down Expand Up @@ -104,3 +106,31 @@ func (h *AuthHandler) VerifyEmail() echo.HandlerFunc {
return response.SendStatusOkResponse(c, "Email berhasil diverifikasi!, Silahkan login.")
}
}

func (h *AuthHandler) ResendOTP() echo.HandlerFunc {
return func(c echo.Context) error {
var emailRequest domain.ResendOTPRequest

if err := c.Bind(&emailRequest); err != nil {
return response.SendErrorResponse(c, http.StatusBadRequest, "Format input yang Anda masukkan tidak sesuai.")
}

if err := utils.ValidateStruct(emailRequest); err != nil {
return response.SendErrorResponse(c, http.StatusBadRequest, "Validasi gagal: "+err.Error())
}

newOTP, err := h.service.ResendOTP(emailRequest.Email)
if err != nil {
return response.SendErrorResponse(c, http.StatusBadRequest, "Gagal mengirim OTP: "+err.Error())
}

err = email.EmaiilService(emailRequest.Email, newOTP.OTP)
if err != nil {
return response.SendErrorResponse(c, http.StatusBadRequest, "Gagal mengirim OTP ke email: "+err.Error())

}

return response.SendStatusOkResponse(c, "OTP berhasil dikirim kembali!, Silahkan cek email anda.")

}
}
3 changes: 3 additions & 0 deletions module/auth/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ type RepositoryAuthInterface interface {
UpdateUser(user *domain.UserModels) (*domain.UserModels, error)
FindValidOTP(userID int, otp string) (*domain.OTPModels, error)
DeleteOTP(otp *domain.OTPModels) error
DeleteUserOTP(userId uint64) error
}

type ServiceAuthInterface interface {
Register(newData *domain.UserModels) (*domain.UserModels, error)
Login(email, password string) (*domain.UserModels, string, error)
VerifyEmail(email, otp string) error
ResendOTP(email string) (*domain.OTPModels, error)
}

type HandlerAuthInterface interface {
Register() echo.HandlerFunc
Login() echo.HandlerFunc
VerifyEmail() echo.HandlerFunc
ResendOTP() echo.HandlerFunc
}
11 changes: 10 additions & 1 deletion module/auth/repository/repository.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package repository

import (
"time"

"github.com/capstone-kelompok-7/backend-disappear/module/auth"
"github.com/capstone-kelompok-7/backend-disappear/module/users/domain"
"gorm.io/gorm"
"time"
)

type AuthRepository struct {
Expand Down Expand Up @@ -65,3 +66,11 @@ func (r *AuthRepository) DeleteOTP(otp *domain.OTPModels) error {
}
return nil
}

func (r *AuthRepository) DeleteUserOTP(userId uint64) error {
if err := r.db.Where("user_id = ?", userId).Delete(&domain.OTPModels{}).Error; err != nil {
return err
}

return nil
}
29 changes: 28 additions & 1 deletion module/auth/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package service

import (
"errors"
"time"

"github.com/capstone-kelompok-7/backend-disappear/module/auth"
"github.com/capstone-kelompok-7/backend-disappear/module/users"
"github.com/capstone-kelompok-7/backend-disappear/module/users/domain"
"github.com/capstone-kelompok-7/backend-disappear/utils"
"github.com/capstone-kelompok-7/backend-disappear/utils/email"
"github.com/capstone-kelompok-7/backend-disappear/utils/otp"
"time"
)

type AuthService struct {
Expand Down Expand Up @@ -114,3 +115,29 @@ func (s *AuthService) VerifyEmail(email, otp string) error {

return nil
}

func (s *AuthService) ResendOTP(email string) (*domain.OTPModels, error) {
user, err := s.userService.GetUsersByEmail(email)
if err != nil {
return nil, err
}
if user.ID == 0 {
return nil, errors.New("user tidak ditemukan pada email ini")
}
errDeleteOTP := s.repo.DeleteUserOTP(user.ID)
if errDeleteOTP != nil {
return nil, errDeleteOTP
}
generateOTP := otp.GenerateRandomOTP(6)
newOTP := &domain.OTPModels{
UserID: int(user.ID),
OTP: generateOTP,
ExpiredOTP: time.Now().Add(2 * time.Minute).Unix(),
}

_, err = s.repo.SaveOTP(newOTP)
if err != nil {
return nil, err
}
return newOTP, nil
}
1 change: 1 addition & 0 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func RouteAuth(e *echo.Echo, h auth.HandlerAuthInterface) {
e.POST("api/v1/auth/register", h.Register())
e.POST("api/v1/auth/login", h.Login())
e.POST("/api/v1/auth/verify", h.VerifyEmail())
e.POST("/api/v1/auth/resend-otp", h.ResendOTP())
}

func RouteUser(e *echo.Echo, h users.HandlerUserInterface, jwtService utils.JWTInterface, userService users.ServiceUserInterface) {
Expand Down

0 comments on commit 8191c3b

Please sign in to comment.