endpoint to update JWT cookies

This commit is contained in:
Daniel Goc
2026-03-18 15:40:54 +01:00
parent 01c8f4333f
commit e094865fc7
7 changed files with 167 additions and 30 deletions

View File

@@ -408,9 +408,12 @@ func (h *AuthHandler) GoogleCallback(c fiber.Ctx) error {
// Redirect to the locale-prefixed charts page after successful Google login.
// The user's preferred language is stored in the auth response; fall back to "en".
lang := response.User.Lang
if lang == "" {
lang = "en"
lang, err := h.authService.GetLangISOCode(response.User.LangID)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadLangID)).JSON(fiber.Map{
"error": responseErrors.GetErrorCode(c, responseErrors.ErrBadLangID),
})
}
return c.Redirect().To(h.config.App.BaseURL + "/" + lang)
}

View File

@@ -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)))
}