Skip to content
2 changes: 1 addition & 1 deletion ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ func Test_Ctx_Cookie_Invalid(t *testing.T) {
{Name: "i", Value: "b", Domain: "2001:db8::1"}, // ipv6 not allowed
{Name: "p", Value: "b", Path: "\x00"}, // invalid path byte
{Name: "e", Value: "b", Expires: time.Date(1500, 1, 1, 0, 0, 0, 0, time.UTC)}, // invalid expires
{Name: "s", Value: "b", Partitioned: true}, // partitioned but not secure
// Note: Partitioned without Secure is auto-fixed (Secure=true set automatically per CHIPS spec)
}

for _, invalid := range cases {
Expand Down
19 changes: 17 additions & 2 deletions docs/api/ctx.md
Original file line number Diff line number Diff line change
Expand Up @@ -1760,8 +1760,7 @@ app.Get("/set", func(c fiber.Ctx) error {
app.Get("/delete", func(c fiber.Ctx) error {
c.Cookie(&fiber.Cookie{
Name: "token",
// Set expiry date to the past
Expires: time.Now().Add(-(time.Hour * 2)),
Expires: fasthttp.CookieExpireDelete, // Use fasthttp's built-in constant
HTTPOnly: true,
SameSite: "Lax",
})
Expand All @@ -1770,6 +1769,22 @@ app.Get("/delete", func(c fiber.Ctx) error {
})
```

You can also use `c.Cookie()` to expire cookies with specific `Path` or `Domain` attributes:

```go title="Example"
app.Get("/logout", func(c fiber.Ctx) error {
// Expire a cookie with path and domain
c.Cookie(&fiber.Cookie{
Name: "token",
Path: "/api",
Domain: "example.com",
Expires: fasthttp.CookieExpireDelete,
})

return c.SendStatus(fiber.StatusOK)
})
```

### Cookie

Sets a cookie.
Expand Down
5 changes: 5 additions & 0 deletions res.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ func (r *DefaultRes) Cookie(cookie *Cookie) {
sameSite = http.SameSiteLaxMode
}

// Partitioned requires Secure=true per CHIPS spec
if cookie.Partitioned {
cookie.Secure = true
}

// create/validate cookie using net/http
hc := &http.Cookie{
Name: cookie.Name,
Expand Down
Loading