Skip to content

Commit 4c73b2d

Browse files
authored
refactor: Migrate Session middleware to new extractors package (#3744)
1 parent aedffa1 commit 4c73b2d

File tree

11 files changed

+194
-658
lines changed

11 files changed

+194
-658
lines changed

docs/middleware/session.md

Lines changed: 151 additions & 167 deletions
Large diffs are not rendered by default.

middleware/csrf/csrf_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/gofiber/fiber/v3"
12+
"github.com/gofiber/fiber/v3/extractors"
1213
"github.com/gofiber/fiber/v3/middleware/session"
1314
utils "github.com/gofiber/utils/v2"
1415
"github.com/stretchr/testify/require"
@@ -76,7 +77,7 @@ func Test_CSRF_WithSession(t *testing.T) {
7677

7778
// session store
7879
store := session.NewStore(session.Config{
79-
Extractor: session.FromCookie("_session"),
80+
Extractor: extractors.FromCookie("_session"),
8081
})
8182

8283
// fiber instance
@@ -274,7 +275,7 @@ func Test_CSRF_ExpiredToken_WithSession(t *testing.T) {
274275

275276
// session store
276277
store := session.NewStore(session.Config{
277-
Extractor: session.FromCookie("_session"),
278+
Extractor: extractors.FromCookie("_session"),
278279
})
279280

280281
// fiber instance
@@ -1112,7 +1113,7 @@ func Test_CSRF_DeleteToken_WithSession(t *testing.T) {
11121113

11131114
// session store
11141115
store := session.NewStore(session.Config{
1115-
Extractor: session.FromCookie("_session"),
1116+
Extractor: extractors.FromCookie("_session"),
11161117
})
11171118

11181119
// fiber instance

middleware/session/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"time"
55

66
"github.com/gofiber/fiber/v3"
7+
"github.com/gofiber/fiber/v3/extractors"
78
"github.com/gofiber/fiber/v3/log"
89
utils "github.com/gofiber/utils/v2"
910
)
@@ -50,9 +51,10 @@ type Config struct {
5051
CookieSameSite string
5152

5253
// Extractor is used to extract the session ID from the request.
54+
// See: https://docs.gofiber.io/guide/extractors
5355
//
54-
// Optional. Default: FromCookie("session_id")
55-
Extractor Extractor
56+
// Optional. Default: extractors.FromCookie("session_id")
57+
Extractor extractors.Extractor
5658

5759
// IdleTimeout defines the maximum duration of inactivity before the session expires.
5860
//
@@ -91,7 +93,7 @@ type Config struct {
9193
var ConfigDefault = Config{
9294
IdleTimeout: 30 * time.Minute,
9395
KeyGenerator: utils.UUIDv4,
94-
Extractor: FromCookie("session_id"),
96+
Extractor: extractors.FromCookie("session_id"),
9597
CookieSameSite: "Lax",
9698
}
9799

middleware/session/config_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/gofiber/fiber/v3"
8+
"github.com/gofiber/fiber/v3/extractors"
89
"github.com/stretchr/testify/require"
910
"github.com/valyala/fasthttp"
1011
)
@@ -23,7 +24,7 @@ func TestConfigDefaultWithCustomConfig(t *testing.T) {
2324
// Test custom config
2425
customConfig := Config{
2526
IdleTimeout: 48 * time.Hour,
26-
Extractor: FromHeader("X-Custom-Session"),
27+
Extractor: extractors.FromHeader("X-Custom-Session"),
2728
KeyGenerator: func() string { return "custom_key" },
2829
}
2930
cfg := configDefault(customConfig)

middleware/session/extractors.go

Lines changed: 0 additions & 224 deletions
This file was deleted.

0 commit comments

Comments
 (0)