send all files
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user