send all files

This commit is contained in:
2024-07-21 00:15:14 -03:00
parent 1e8cda0139
commit c9464c4a6f
72 changed files with 6335 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
package authmodule
import (
authdto "api/application/auth/dto"
"api/models/api"
)
type Controller interface {
Authorization(data *authdto.AuthDto) (*api.Response, int, error)
}
type controller struct {
AuthService
}
func newController(service AuthService) Controller {
return &controller{
AuthService: service,
}
}
func (c *controller) Authorization(data *authdto.AuthDto) (*api.Response, int, error) {
return c.AuthService.Authorization(data)
}
+34
View File
@@ -0,0 +1,34 @@
package authdto
import (
"api/libs/logger"
"errors"
"github.com/go-playground/validator/v10"
)
type AuthDto struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
}
func (d *AuthDto) Validate() error {
validate := validator.New()
err := validate.Struct(d)
if err != nil {
// this check is only needed when your code could produce
// an invalid value for validation such as interface with nil
// value most including myself do not usually have code like this.
if _, ok := err.(*validator.InvalidValidationError); ok {
logger.Development.Info(err.Error())
}
for _, e := range err.(validator.ValidationErrors) {
err = errors.New(e.Field() + " " + e.Tag())
}
}
return err
}
+10
View File
@@ -0,0 +1,10 @@
package authmodule
import "github.com/gofiber/fiber/v2"
// apply routes in app fiber, with controllers and services defined
func AuthModuleDecorator(app *fiber.App) {
s := newService()
c := newController(s)
newRoutes(c, app)
}
+51
View File
@@ -0,0 +1,51 @@
package authmodule
import (
authdto "api/application/auth/dto"
"api/libs/logger"
"api/models/api"
"github.com/gofiber/fiber/v2"
)
func newRoutes(c Controller, app *fiber.App) {
app.Post("/authorization", authorization(c))
}
func authorization(controller Controller) func(*fiber.Ctx) error {
return func(c *fiber.Ctx) error {
auth := &authdto.AuthDto{}
err := c.BodyParser(auth)
if err != nil {
logger.Production.Info(err.Error())
c.Status(fiber.StatusBadRequest)
return c.JSON(api.Response{
Error: true,
ErrorMessage: err.Error(),
})
}
err = auth.Validate()
if err != nil {
logger.Production.Info(err.Error())
c.Status(fiber.StatusBadRequest)
return c.JSON(api.Response{
Error: true,
ErrorMessage: err.Error(),
})
}
response, statusCode, err := controller.Authorization(auth)
if err != nil {
logger.Production.Info(err.Error())
c.Status(statusCode)
return c.JSON(api.Response{
Error: true,
ErrorMessage: err.Error(),
})
}
return c.Status(statusCode).JSON(response)
}
}
+69
View File
@@ -0,0 +1,69 @@
package authmodule
import (
authdto "api/application/auth/dto"
"api/libs/jwt"
"api/libs/postgres"
"api/models/api"
"api/models/users"
"context"
"github.com/alexedwards/argon2id"
"github.com/gofiber/fiber/v2"
)
type AuthService interface {
Authorization(data *authdto.AuthDto) (*api.Response, int, error)
}
type authService struct {
userRepository *users.Queries
ctx context.Context
}
func newService() AuthService {
return &authService{
userRepository: users.New(postgres.Pool),
ctx: context.Background(),
}
}
func (a *authService) Authorization(data *authdto.AuthDto) (*api.Response, int, error) {
userL, err := a.userRepository.GetUser(a.ctx, data.Username)
if err != nil {
return &api.Response{Error: true, ErrorMessage: err.Error()}, fiber.StatusUnauthorized, err
}
match, err := argon2id.ComparePasswordAndHash(data.Password, userL.Password)
if err != nil {
return &api.Response{Error: true, ErrorMessage: err.Error()}, fiber.StatusUnauthorized, err
}
if match {
dic := map[string]interface{}{
"id": userL.User,
}
token, err := jwt.EncodeJwt(dic)
if err != nil {
return &api.Response{Error: true, ErrorMessage: err.Error()}, fiber.StatusInternalServerError, err
}
payload := struct {
User struct {
Id string `json:"id"`
Username string `json:"username"`
} `json:"user"`
Token string `json:"token"`
}{}
payload.User.Username = userL.Username
payload.User.Id = userL.User
payload.Token = token
return &api.Response{Error: false, Payload: payload}, fiber.StatusOK, err
}
return nil, fiber.StatusUnauthorized, nil
}