endpoint to update JWT cookies
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package restricted
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.ma-al.com/goc_daniel/b2b/app/service/authService"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/service/jwtService"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
|
||||
@@ -11,14 +14,17 @@ import (
|
||||
|
||||
// JWTCookiesHandler for updating JWT cookies.
|
||||
type JWTCookiesHandler struct {
|
||||
jwtService *jwtService.JWTService
|
||||
jwtService *jwtService.JWTService
|
||||
authService *authService.AuthService
|
||||
}
|
||||
|
||||
// NewJWTCookiesHandler creates a new JWTCookiesHandler instance
|
||||
func NewJWTCookiesHandler() *JWTCookiesHandler {
|
||||
jwtService := jwtService.New()
|
||||
authSvc := authService.NewAuthService()
|
||||
return &JWTCookiesHandler{
|
||||
jwtService: jwtService,
|
||||
jwtService: jwtService,
|
||||
authService: authSvc,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,5 +59,57 @@ func (h *JWTCookiesHandler) GetCountries(c fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func (h *JWTCookiesHandler) UpdateChoice(c fiber.Ctx) error {
|
||||
return nil
|
||||
// Get user ID from JWT claims in context (set by auth middleware)
|
||||
claims, ok := c.Locals("jwt_claims").(*authService.JWTClaims)
|
||||
if !ok || claims == nil {
|
||||
return c.Status(fiber.StatusUnauthorized).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrNotAuthenticated)))
|
||||
}
|
||||
|
||||
// Parse language and country_id from query params
|
||||
langIDStr := c.Query("lang_id")
|
||||
countryIDStr := c.Query("country_id")
|
||||
|
||||
var langID uint
|
||||
if langIDStr != "" {
|
||||
parsedID, err := strconv.ParseUint(langIDStr, 10, 32)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadLangID)))
|
||||
}
|
||||
langID = uint(parsedID)
|
||||
} else {
|
||||
langID = 0
|
||||
}
|
||||
|
||||
var countryID uint
|
||||
if countryIDStr != "" {
|
||||
parsedID, err := strconv.ParseUint(countryIDStr, 10, 32)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadCountryID)))
|
||||
}
|
||||
countryID = uint(parsedID)
|
||||
} else {
|
||||
countryID = 0
|
||||
}
|
||||
|
||||
// Update choice and get new token using AuthService
|
||||
newToken, err := h.authService.UpdateChoice(claims.UserID, langID, countryID)
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
// Set the new JWT cookie
|
||||
cookie := new(fiber.Cookie)
|
||||
cookie.Name = "jwt_token"
|
||||
cookie.Value = newToken
|
||||
cookie.HTTPOnly = true
|
||||
cookie.Secure = true
|
||||
cookie.SameSite = fiber.CookieSameSiteLaxMode
|
||||
|
||||
c.Cookie(cookie)
|
||||
|
||||
return c.JSON(response.Make(&fiber.Map{"token": newToken}, 0, i18n.T_(c, response.Message_OK)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user