Skip to content

Commit 3609930

Browse files
committed
Enhance documentation for session management; clarify usage of Session.use_store and configuration for various storage backends
1 parent 83dbea3 commit 3609930

4 files changed

Lines changed: 34 additions & 15 deletions

File tree

docs/features/type-safe-sessions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class UserSession < Session::Base
1313
end
1414
```
1515

16-
The `authenticated?` method is abstract in `Session::Base` and must be implemented. Using `property?` is the simplest approach, but you can also define it manually:
16+
`Session::Base` provides a default `authenticated?` that returns `false`. Override it with your own logic:
1717

1818
```crystal
1919
class UserSession < Session::Base

docs/getting-started/installation.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ class TestSession < Session::Base
3636
property test : String = "Hello"
3737
end
3838
39+
Session.use_store(Session::MemoryStore(TestSession))
40+
3941
Session.configure do |config|
4042
config.secret = "test-secret-for-verification"
4143
config.store = Session::MemoryStore(TestSession).new
4244
end
4345
44-
store = Session.config.store.not_nil!
46+
store = Session.config.store!
4547
session = store.create
4648
puts "Session ID: #{session.session_id}"
4749
puts "Installation successful!"

docs/getting-started/quick-start.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ end
2828

2929
## Step 2: Configure Session
3030

31-
Set up the session configuration:
31+
Declare the store type with `Session.use_store`, then configure:
3232

3333
```crystal
34+
# Tell the library which concrete store type you'll use.
35+
# This must be called before Session.configure.
36+
Session.use_store(Session::MemoryStore(UserSession))
37+
3438
Session.configure do |config|
3539
# Required: Secret key for encryption (use environment variable in production)
3640
config.secret = ENV["SESSION_SECRET"]? || "your-32-character-secret-key-here"
@@ -41,17 +45,22 @@ Session.configure do |config|
4145
# Cookie name
4246
config.session_key = "myapp_session"
4347
44-
# Choose a storage backend
48+
# Assign a store instance matching the type declared above
4549
config.store = Session::MemoryStore(UserSession).new
4650
end
4751
```
4852

53+
> **Why `use_store`?** Crystal's generic type system is invariant: a
54+
> `MemoryStore(UserSession)` cannot be assigned to `Store(Base)?`. `use_store`
55+
> injects a typed `store` property into `Configuration` so the compiler knows
56+
> the exact type.
57+
4958
## Step 3: Use Sessions
5059

5160
### Create a Session
5261

5362
```crystal
54-
store = Session.config.store.not_nil!
63+
store = Session.config.store!
5564
5665
# Create a new session
5766
session = store.create
@@ -100,13 +109,15 @@ Integrate with an HTTP server:
100109
require "http/server"
101110
require "session"
102111
103-
# Configure session
112+
# Declare store type, then configure
113+
Session.use_store(Session::MemoryStore(UserSession))
114+
104115
Session.configure do |config|
105116
config.secret = ENV["SESSION_SECRET"]
106117
config.store = Session::MemoryStore(UserSession).new
107118
end
108119
109-
store = Session.config.store.not_nil!
120+
store = Session.config.store!
110121
111122
# Create server with session handler
112123
server = HTTP::Server.new([
@@ -175,15 +186,17 @@ class UserSession < Session::Base
175186
end
176187
end
177188
178-
# Configure
189+
# Declare store type, then configure
190+
Session.use_store(Session::MemoryStore(UserSession))
191+
179192
Session.configure do |config|
180193
config.secret = ENV["SESSION_SECRET"]? || "dev-secret-32-characters-long!!"
181194
config.timeout = 1.hour
182195
config.sliding_expiration = true
183196
config.store = Session::MemoryStore(UserSession).new
184197
end
185198
186-
store = Session.config.store.not_nil!
199+
store = Session.config.store!
187200
188201
# Server
189202
server = HTTP::Server.new([

docs/storage-backends/overview.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ graph TD
3838
### Cookie Store
3939

4040
```crystal
41+
Session.use_store(Session::CookieStore(UserSession))
42+
# then inside Session.configure:
4143
config.store = Session::CookieStore(UserSession).new
4244
```
4345

@@ -49,6 +51,8 @@ config.store = Session::CookieStore(UserSession).new
4951
### Memory Store
5052

5153
```crystal
54+
Session.use_store(Session::MemoryStore(UserSession))
55+
# then inside Session.configure:
5256
config.store = Session::MemoryStore(UserSession).new
5357
```
5458

@@ -60,9 +64,9 @@ config.store = Session::MemoryStore(UserSession).new
6064
### Redis Store
6165

6266
```crystal
63-
config.store = Session::RedisStore(UserSession).new(
64-
client: Redis.new
65-
)
67+
Session.use_store(Session::RedisStore(UserSession))
68+
# then inside Session.configure:
69+
config.store = Session::RedisStore(UserSession).new(client: Redis.new)
6670
```
6771

6872
**Use when:**
@@ -73,10 +77,10 @@ config.store = Session::RedisStore(UserSession).new(
7377
### Clustered Redis Store
7478

7579
```crystal
80+
Session.use_store(Session::ClusteredRedisStore(UserSession))
81+
# then inside Session.configure:
7682
config.cluster.enabled = true
77-
config.store = Session::ClusteredRedisStore(UserSession).new(
78-
client: Redis.new
79-
)
83+
config.store = Session::ClusteredRedisStore(UserSession).new(client: Redis.new)
8084
```
8185

8286
**Use when:**

0 commit comments

Comments
 (0)