package server

import (
	def "git.pbiernat.dev/egommerce/identity-service/internal/app/definition"
	"git.pbiernat.dev/egommerce/identity-service/internal/app/service"
	"github.com/gofiber/fiber/v2"
)

func (s *Server) LoginHandler(c *fiber.Ctx) error {
	data := new(def.AuthLoginRequest)
	if err := c.BodyParser(data); err != nil {
		return err
	}

	token, err := service.AuthService.Login(data.Username, data.Password)
	if err != nil {
		return fiber.NewError(fiber.StatusUnauthorized)
	}

	cookie := service.AuthService.Cookie("auth_token", token)
	c.Cookie(cookie)

	return c.JSON(&def.AuthLoginResponse{JWTToken: token})
}
