cleanup
This commit is contained in:
@@ -40,6 +40,7 @@ func AuthHandlerRoutes(r fiber.Router) fiber.Router {
|
||||
r.Post("/reset-password", handler.ResetPassword)
|
||||
r.Post("/logout", handler.Logout)
|
||||
r.Post("/refresh", handler.RefreshToken)
|
||||
r.Post("/update-choice", handler.UpdateJWTToken)
|
||||
|
||||
// Google OAuth2
|
||||
r.Get("/google", handler.GoogleLogin)
|
||||
@@ -344,6 +345,11 @@ func (h *AuthHandler) CompleteRegistration(c fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusCreated).JSON(response)
|
||||
}
|
||||
|
||||
// CompleteRegistration handles completion of registration with password
|
||||
func (h *AuthHandler) UpdateJWTToken(c fiber.Ctx) error {
|
||||
return h.UpdateJWTToken(c)
|
||||
}
|
||||
|
||||
// GoogleLogin redirects the user to Google's OAuth2 consent page
|
||||
func (h *AuthHandler) GoogleLogin(c fiber.Ctx) error {
|
||||
// Generate a random state token and store it in a short-lived cookie
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
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"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/response"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// JWTCookiesHandler for updating JWT cookies.
|
||||
type JWTCookiesHandler struct {
|
||||
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,
|
||||
authService: authSvc,
|
||||
}
|
||||
}
|
||||
|
||||
func JWTCookiesHandlerRoutes(r fiber.Router) fiber.Router {
|
||||
handler := NewJWTCookiesHandler()
|
||||
|
||||
r.Get("/get-languages", handler.GetLanguages)
|
||||
r.Get("/get-countries", handler.GetCountries)
|
||||
r.Get("/update-choice", handler.UpdateChoice)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (h *JWTCookiesHandler) GetLanguages(c fiber.Ctx) error {
|
||||
languages, err := h.jwtService.GetLanguages()
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
return c.JSON(response.Make(&languages, 0, i18n.T_(c, response.Message_OK)))
|
||||
}
|
||||
|
||||
func (h *JWTCookiesHandler) GetCountries(c fiber.Ctx) error {
|
||||
countries, err := h.jwtService.GetCountriesAndCurrencies()
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
return c.JSON(response.Make(&countries, 0, i18n.T_(c, response.Message_OK)))
|
||||
}
|
||||
|
||||
func (h *JWTCookiesHandler) UpdateChoice(c fiber.Ctx) error {
|
||||
// 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)))
|
||||
}
|
||||
52
app/delivery/web/api/restricted/langsAndCountries.go
Normal file
52
app/delivery/web/api/restricted/langsAndCountries.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package restricted
|
||||
|
||||
import (
|
||||
"git.ma-al.com/goc_daniel/b2b/app/service/langsAndCountriesService"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/response"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// LangsAndCountriesHandler for getting languages and countries data
|
||||
type LangsAndCountriesHandler struct {
|
||||
langsAndCountriesService *langsAndCountriesService.LangsAndCountriesService
|
||||
}
|
||||
|
||||
// NewLangsAndCountriesHandler creates a new LangsAndCountriesHandler instance
|
||||
func NewLangsAndCountriesHandler() *LangsAndCountriesHandler {
|
||||
langsAndCountriesService := langsAndCountriesService.New()
|
||||
return &LangsAndCountriesHandler{
|
||||
langsAndCountriesService: langsAndCountriesService,
|
||||
}
|
||||
}
|
||||
|
||||
func LangsAndCountriesHandlerRoutes(r fiber.Router) fiber.Router {
|
||||
handler := NewLangsAndCountriesHandler()
|
||||
|
||||
r.Get("/get-languages", handler.GetLanguages)
|
||||
r.Get("/get-countries", handler.GetCountries)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (h *LangsAndCountriesHandler) GetLanguages(c fiber.Ctx) error {
|
||||
languages, err := h.langsAndCountriesService.GetLanguages()
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
return c.JSON(response.Make(&languages, 0, i18n.T_(c, response.Message_OK)))
|
||||
}
|
||||
|
||||
func (h *LangsAndCountriesHandler) GetCountries(c fiber.Ctx) error {
|
||||
countries, err := h.langsAndCountriesService.GetCountriesAndCurrencies()
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
return c.JSON(response.Make(&countries, 0, i18n.T_(c, response.Message_OK)))
|
||||
}
|
||||
@@ -99,8 +99,8 @@ func (s *Server) Setup() error {
|
||||
|
||||
// changing the JWT cookies routes (restricted)
|
||||
// in reality it just handles changing user's country and language
|
||||
jwtUpdates := s.restricted.Group("/jwt-updates")
|
||||
restricted.JWTCookiesHandlerRoutes(jwtUpdates)
|
||||
langsAndCountries := s.restricted.Group("/langs-and-countries")
|
||||
restricted.LangsAndCountriesHandlerRoutes(langsAndCountries)
|
||||
|
||||
// // Restricted routes example
|
||||
// restricted := s.api.Group("/restricted")
|
||||
|
||||
Reference in New Issue
Block a user