Skip to content

Commit ff90add

Browse files
committed
Update CHANGELOG.md and README.md for version 1.4.0
1 parent b3c3be4 commit ff90add

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.4.0 (03-02-2025)
2+
3+
- Prevent empty sessions from being stored (lazy session creation) [#113](https://github.com/kemalcr/kemal-session/pull/113) Thanks @sdogruyol :pray:
4+
- Add flash functionality [#112](https://github.com/kemalcr/kemal-session/pull/112). Thanks @sdogruyol :pray:
5+
16
# 1.3.0 (03-08-2025)
27

38
- Add option for per-session CSRF token [#105](https://github.com/kemalcr/kemal-session/pull/105). Thanks @swsch :pray:

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,68 @@ csrf_handler = Kemal::Session::CSRF.new(
175175
add_handler csrf_handler
176176
```
177177

178+
## 💬 Flash Messages
179+
180+
Flash messages are one-time messages that persist across a single redirect—ideal for success notices, errors, or warnings after form submissions.
181+
182+
- **Set** a flash message in one request (e.g. after an action).
183+
- **Read** it in the next request (e.g. the redirected page); the value is then removed automatically.
184+
185+
### Basic Usage
186+
187+
```crystal
188+
require "kemal"
189+
require "kemal-session"
190+
191+
Kemal::Session.config.secret = "my-secret-key"
192+
193+
# Set a flash message (e.g. after login or form submit)
194+
post "/login" do |env|
195+
# ... authenticate user ...
196+
env.flash["notice"] = "Welcome back!"
197+
env.redirect "/dashboard"
198+
end
199+
200+
# Read the flash in the next request (e.g. layout or target page)
201+
get "/dashboard" do |env|
202+
notice = env.flash["notice"]? # returns value and clears it
203+
error = env.flash["error"]? # optional: nil if key missing
204+
205+
message = notice ? "<p class=\"notice\">#{notice}</p>" : ""
206+
message += error ? "<p class=\"error\">#{error}</p>" : ""
207+
"Dashboard\n#{message}"
208+
end
209+
```
210+
211+
### API
212+
213+
| Operation | Method | Behavior |
214+
|-----------|--------|----------|
215+
| Set | `env.flash["key"] = "value"` | Stores the message for the next request |
216+
| Read (optional) | `env.flash["key"]?` | Returns the value and removes it; returns `nil` if key is missing |
217+
| Read (required) | `env.flash["key"]` | Returns the value and removes it; raises `KeyError` if key is missing |
218+
219+
Use `env.flash["key"]?` when the key might not be set (e.g. optional notices). Use `env.flash["key"]` when you expect the key to exist; it will raise if the message was already consumed or never set.
220+
221+
### Example: Form with Success/Error
222+
223+
```crystal
224+
post "/contact" do |env|
225+
if send_email(env.params.body)
226+
env.flash["notice"] = "Message sent successfully."
227+
else
228+
env.flash["error"] = "Failed to send message."
229+
end
230+
env.redirect "/contact"
231+
end
232+
233+
get "/contact" do |env|
234+
notice = env.flash["notice"]?
235+
error = env.flash["error"]?
236+
# Render form and show notice/error above it
237+
end
238+
```
239+
178240
## 📊 Supported Data Types
179241

180242
Kemal Session supports all common Crystal types with intuitive method names:

0 commit comments

Comments
 (0)