Spaces:
Running
Running
File size: 2,098 Bytes
ab82ffd c0722d3 ab82ffd c0722d3 ab82ffd c0722d3 ab82ffd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package controllers
import (
"math/rand"
"time"
"abdanhafidz.com/go-boilerplate/models/dto"
"abdanhafidz.com/go-boilerplate/services"
"github.com/gin-gonic/gin"
)
type ForgotPasswordController interface {
Request(ctx *gin.Context)
Reset(ctx *gin.Context)
}
type forgotPasswordController struct {
forgotPasswordService services.ForgotPasswordService
}
func NewForgotPasswordController(forgotPasswordService services.ForgotPasswordService) ForgotPasswordController {
return &forgotPasswordController{forgotPasswordService: forgotPasswordService}
}
// Request Forgot Password godoc
// @Summary Request Password Reset
// @Description Generate a password reset token and send it to the specified email address
// @Tags Forgot Password
// @Accept json
// @Produce json
// @Param request body dto.ForgotPasswordRequest true "Forgot Password Request"
// @Success 200 {object} dto.SuccessResponse[models.ForgotPassword]
// @Failure 400 {object} dto.ErrorResponse
// @Router /api/v1/authentication/forgot-password [post]
func (c *forgotPasswordController) Request(ctx *gin.Context) {
req := RequestJSON[dto.ForgotPasswordRequest](ctx)
token := uint(rand.Intn(900000) + 100000)
due := time.Now().Add(15 * time.Minute)
res, err := c.forgotPasswordService.Request(ctx.Request.Context(), req.Email, token, due)
ResponseJSON(ctx, req, res, err)
}
// Reset Forgot Password godoc
// @Summary Reset Password
// @Description Reset the user's password using the provided token
// @Tags Forgot Password
// @Accept json
// @Produce json
// @Param request body dto.ResetPasswordRequest true "Reset Password Request"
// @Success 200 {object} dto.SuccessResponse[any]
// @Failure 400 {object} dto.ErrorResponse
func (c *forgotPasswordController) Reset(ctx *gin.Context) {
req := RequestJSON[dto.ResetPasswordRequest](ctx)
err := c.forgotPasswordService.Reset(ctx.Request.Context(), req.Token, req.NewPassword)
ResponseJSON[any](ctx, req, gin.H{"status": "ok"}, err)
}
|