File size: 1,276 Bytes
b025147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}
}

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)
}

func (c *forgotPasswordController) Reset(ctx *gin.Context) {
	type resetReq struct {
		Token       uint   `json:"token" binding:"required"`
		NewPassword string `json:"new_password" binding:"required"`
	}
	req := RequestJSON[resetReq](ctx)
	err := c.forgotPasswordService.Reset(ctx.Request.Context(), req.Token, req.NewPassword)
	ResponseJSON[any](ctx, req, gin.H{"status": "ok"}, err)
}