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 }