File size: 2,698 Bytes
b025147
 
 
 
b55a115
 
b025147
 
b55a115
b025147
 
 
 
 
 
 
b55a115
b025147
 
 
 
 
 
 
171ae7c
b025147
171ae7c
 
b025147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c9198b
b025147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171ae7c
b025147
 
b55a115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package controllers

import (
	"abdanhafidz.com/go-boilerplate/models/dto"
	entity "abdanhafidz.com/go-boilerplate/models/entity"
	http_error "abdanhafidz.com/go-boilerplate/models/error"
	"abdanhafidz.com/go-boilerplate/services"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

type AuthenticationController interface {
	SignUp(ctx *gin.Context)
	SignIn(ctx *gin.Context)
	ExternalAuth(ctx *gin.Context)
	ChangePassword(ctx *gin.Context)
	UpdateUserRole(ctx *gin.Context)
}

type authenticationController struct {
	accountService      services.AccountService
	externalAuthService services.ExternalAuthService
}

func NewAuthenticationController(accountService services.AccountService, externalAuthService services.ExternalAuthService) AuthenticationController {
	return &authenticationController{
		accountService:      accountService,
		externalAuthService: externalAuthService,
	}
}
func (c *authenticationController) SignUp(ctx *gin.Context) {
	req := RequestJSON[dto.SignUpRequest](ctx)
	res, err := c.accountService.Create(ctx.Request.Context(), req.Name, req.Email, req.Username, req.Password)
	ResponseJSON(ctx, req, res, err)
}

func (c *authenticationController) SignIn(ctx *gin.Context) {
	req := RequestJSON[dto.SignInRequest](ctx)
	res, err := c.accountService.Validate(ctx, req.EmailorUsername, req.Password)
	ResponseJSON(ctx, req, res, err)
}

func (c *authenticationController) ChangePassword(ctx *gin.Context) {
	req := RequestJSON[dto.ChangePasswordRequest](ctx)
	accountId := ParseAccountId(ctx)
	res, err := c.accountService.ChangePassword(ctx.Request.Context(), accountId, req.OldPassword, req.NewPassword)
	ResponseJSON(ctx, req, res, err)
}

func (c *authenticationController) ExternalAuth(ctx *gin.Context) {
	req := RequestJSON[dto.ExternalAuthRequest](ctx)
	var (
		res dto.AuthenticatedUser
		err error
	)

	switch req.OauthProvider {
	case "google":
		res, err = c.externalAuthService.GoogleAuth(ctx.Request.Context(), req.OauthID)
	default:
		ResponseJSON(ctx, req, dto.AuthenticatedUser{}, nil)
		return
	}

	ResponseJSON(ctx, req, res, err)
}

func (c *authenticationController) UpdateUserRole(ctx *gin.Context) {
	req := RequestJSON[dto.UpdateUserRoleRequest](ctx)
	targetAccountId, err := uuid.Parse(ctx.Param("account_id"))
	if err != nil {
		ResponseJSON(ctx, req, entity.Account{}, http_error.BAD_REQUEST_ERROR)
		return
	}

	acc, err := c.accountService.GetById(ctx.Request.Context(), targetAccountId)
	if err != nil {
		ResponseJSON(ctx, req, entity.Account{}, err)
		return
	}

	accountToUpdate := acc
	accountToUpdate.Role = req.Role

	res, err := c.accountService.Update(ctx.Request.Context(), accountToUpdate)
	ResponseJSON(ctx, req, res, err)
}