clear cookie if failing
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 48s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m25s

This commit is contained in:
2025-12-03 19:51:22 +01:00
parent 6321ad76a1
commit 5ad7131df2
2 changed files with 31 additions and 11 deletions

View File

@@ -210,7 +210,14 @@ func (s *CheckoutPoolServer) StartCheckoutHandler(w http.ResponseWriter, r *http
} }
// Create checkout with same ID as cart // Create checkout with same ID as cart
checkoutId := checkout.CheckoutId(cartId) var checkoutId checkout.CheckoutId = cart.MustNewCartId()
cookie, err := r.Cookie(checkoutCookieName)
if err == nil {
parsed, ok := cart.ParseCartId(cookie.Value)
if ok {
checkoutId = parsed
}
}
// Initialize checkout with cart state wrapped in Any // Initialize checkout with cart state wrapped in Any
cartStateAny := &messages.InitializeCheckout{ cartStateAny := &messages.InitializeCheckout{
@@ -225,13 +232,14 @@ func (s *CheckoutPoolServer) StartCheckoutHandler(w http.ResponseWriter, r *http
result, err := s.ApplyLocal(r.Context(), checkoutId, cartStateAny) result, err := s.ApplyLocal(r.Context(), checkoutId, cartStateAny)
if err != nil { if err != nil {
setCheckoutCookie(w, 0, r.TLS != nil)
logger.Error("failed to initialize checkout", "error", err) logger.Error("failed to initialize checkout", "error", err)
http.Error(w, "failed to initialize checkout", http.StatusInternalServerError) http.Error(w, "failed to initialize checkout", http.StatusInternalServerError)
return return
} }
// Set checkout cookie // Set checkout cookie
setCheckoutCookie(w, result.Result.Id, r.TLS != nil) setCheckoutCookie(w, checkoutId, r.TLS != nil)
if err := s.WriteResult(w, result.Result); err != nil { if err := s.WriteResult(w, result.Result); err != nil {
logger.Error("failed to write result", "error", err) logger.Error("failed to write result", "error", err)

View File

@@ -68,6 +68,17 @@ func (a *CheckoutPoolServer) reserveInventory(ctx context.Context, grain *checko
const checkoutCookieName = "checkoutid" const checkoutCookieName = "checkoutid"
func setCheckoutCookie(w http.ResponseWriter, checkoutId checkout.CheckoutId, tls bool) { func setCheckoutCookie(w http.ResponseWriter, checkoutId checkout.CheckoutId, tls bool) {
if checkoutId == 0 {
http.SetCookie(w, &http.Cookie{
Name: checkoutCookieName,
Value: checkoutId.String(),
Secure: tls,
HttpOnly: true,
Path: "/",
Expires: time.Unix(0, 0),
SameSite: http.SameSiteLaxMode,
})
} else {
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{
Name: checkoutCookieName, Name: checkoutCookieName,
Value: checkoutId.String(), Value: checkoutId.String(),
@@ -78,6 +89,7 @@ func setCheckoutCookie(w http.ResponseWriter, checkoutId checkout.CheckoutId, tl
SameSite: http.SameSiteLaxMode, SameSite: http.SameSiteLaxMode,
}) })
} }
}
func CookieCheckoutIdHandler(fn func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error) func(w http.ResponseWriter, r *http.Request) { func CookieCheckoutIdHandler(fn func(w http.ResponseWriter, r *http.Request, checkoutId checkout.CheckoutId) error) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {