Skip to content

Commit

Permalink
refactor hiher layers done and auth controller almost done execpt res…
Browse files Browse the repository at this point in the history
…et-password service
  • Loading branch information
tahadostifam committed May 25, 2024
1 parent 938922b commit 167d6b9
Show file tree
Hide file tree
Showing 49 changed files with 758 additions and 771 deletions.
20 changes: 15 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
PACKAGES=$(shell go list ./... | grep -v 'tests')

# Install tools are needed for development (golangci-lint, gofumpt)
# Install development tools (golangci-lint, gofumpt)
devtools:
@echo "Installing devtools"
go install github.com/golangci/golangci-lint/cmd/[email protected]
go install mvdan.cc/gofumpt@latest

# GoLang Unit-Test
# Tests
test:
go test ./... -covermode=atomic
go test ./...

# Format
fmt:
Expand All @@ -18,3 +16,15 @@ fmt:
# Linter
check:
golangci-lint run --build-tags "${BUILD_TAG}" --timeout=20m0s

# Run on development
dev:
go run cmd/server/server.go

# Build for production
build:
export GIN_MODE=release
export ENV=production
go mod tidy
go clean -cache
go build -o ./build/server cmd/server/server.go
153 changes: 153 additions & 0 deletions app/controller/auth_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package controller

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/kavkaco/Kavka-Core/app/presenters"
validator "github.com/kavkaco/Kavka-Core/app/validator"
auth "github.com/kavkaco/Kavka-Core/internal/service/auth"
chat "github.com/kavkaco/Kavka-Core/internal/service/chat"
"github.com/kavkaco/Kavka-Core/pkg/email"
"go.uber.org/zap"
)

type AuthController struct {
ctx context.Context
logger *zap.Logger
authService auth.AuthService
chatService chat.ChatService
emailService email.EmailOtp
}

func NewAuthController(ctx context.Context, logger *zap.Logger, authService auth.AuthService, chatService chat.ChatService, emailService email.EmailOtp) *AuthController {
return &AuthController{ctx, logger, authService, chatService, emailService}
}

func (ctrl *AuthController) HandleLogin(c *gin.Context) {
if body := validator.ValidateBody[validator.AuthLoginRequest](c); body != nil {
user, accessToken, refreshToken, err := ctrl.authService.Login(ctrl.ctx, body.Email, body.Password)
if err != nil {
presenters.ErrorResponse(c, err)
return
}

userChats, err := ctrl.chatService.GetUserChats(ctrl.ctx, user.UserID)
if err != nil {
presenters.ErrorResponse(c, err)
return
}

c.Header(presenters.RefreshTokenHeaderName, refreshToken)
c.Header(presenters.AccessTokenHeaderName, accessToken)

c.JSON(http.StatusOK, presenters.UserResponse(c, user, userChats))
}
}

func (ctrl *AuthController) HandleRegister(c *gin.Context) {
if body := validator.ValidateBody[validator.AuthRegisterRequest](c); body != nil {
_, verifyEmailToken, err := ctrl.authService.Register(ctrl.ctx, body.Name, body.LastName, body.Username, body.Email, body.Password)
if err != nil {
presenters.ErrorResponse(c, err)
return
}

// FIXME
fmt.Println(verifyEmailToken)

c.JSON(http.StatusOK, presenters.CodeMessageDto{
Code: 200,
Message: "account registered successfully",
})
}
}

func (ctrl *AuthController) HandleRefreshToken(c *gin.Context) {
refreshToken := c.GetHeader(presenters.RefreshTokenHeaderName)
accessToken := c.GetHeader(presenters.AccessTokenHeaderName)

newAccessToken, err := ctrl.authService.RefreshToken(ctrl.ctx, refreshToken, accessToken)
if errors.Is(err, auth.ErrAccessDenied) {
presenters.AccessDenied(c)
return
} else if err != nil {
presenters.ErrorResponse(c, err)
return
}

c.Header(presenters.AccessTokenHeaderName, newAccessToken)

c.JSON(http.StatusOK, presenters.CodeMessageDto{
Code: 200,
Message: "access token refreshed",
})
}

func (ctrl *AuthController) HandleAuthenticate(c *gin.Context) {
accessToken := c.GetHeader(presenters.AccessTokenHeaderName)

// get the user info
user, err := ctrl.authService.Authenticate(ctrl.ctx, accessToken)
if err != nil {
presenters.AccessDenied(c)
return
}

// gathering user chats
userChats, err := ctrl.chatService.GetUserChats(ctrl.ctx, user.UserID)
if err != nil {
presenters.InternalServerErrorResponse(c)
return
}

err = presenters.UserResponse(c, user, userChats)
if err != nil {
presenters.InternalServerErrorResponse(c)
}
}

func (ctrl *AuthController) HandleVerifyEmail(c *gin.Context) {
redirectTo := c.Query("redirect_to")
verifyEmailToken := c.Param("token")

if verifyEmailToken == "" {
presenters.BadRequestResponse(c)

Check failure on line 118 in app/controller/auth_controller.go

View workflow job for this annotation

GitHub Actions / linting

Error return value of `presenters.BadRequestResponse` is not checked (errcheck)
return
}

err := ctrl.authService.VerifyEmail(ctrl.ctx, verifyEmailToken)
if err != nil {
presenters.ErrorResponse(c, err)
return
}

if redirectTo != "" {
c.Redirect(http.StatusPermanentRedirect, redirectTo)
return
}

c.JSON(http.StatusOK, presenters.CodeMessageDto{
Code: 200,
Message: "email verified",
})
}

func (ctrl *AuthController) HandleChangePassword(c *gin.Context) {
accessToken := c.GetHeader(presenters.AccessTokenHeaderName)
if body := validator.ValidateBody[validator.ChangePasswordRequest](c); body != nil {
err := ctrl.authService.ChangePassword(ctrl.ctx, accessToken, body.OldPassword, body.NewPassword)
if err != nil {
presenters.ErrorResponse(c, err)
return
}

c.JSON(http.StatusOK, presenters.CodeMessageDto{
Code: 200,
Message: "password changed",
})
}
}
108 changes: 0 additions & 108 deletions app/controller/user_controller.go

This file was deleted.

62 changes: 0 additions & 62 deletions app/dto/dto.go

This file was deleted.

10 changes: 0 additions & 10 deletions app/dto/user_dto.go

This file was deleted.

Loading

0 comments on commit 167d6b9

Please sign in to comment.