Skip to content

Commit 19df0cb

Browse files
committed
add flash messages
1 parent 735d8e6 commit 19df0cb

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

ctx.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type DefaultCtx struct {
5353
viewBindMap *dictpool.Dict // Default view map to bind template engine
5454
bind *Bind // Default bind reference
5555
redirect *Redirect // Default redirect reference
56+
flashMessages string // flash messages sent by redirection cookie
5657
}
5758

5859
// Range data for c.Range
@@ -1294,3 +1295,13 @@ func (c *DefaultCtx) Bind() *Bind {
12941295
}
12951296
return c.bind
12961297
}
1298+
1299+
func (c *DefaultCtx) setFlash() {
1300+
c.flashMessages = c.Cookies("fiber_flash")
1301+
1302+
c.ClearCookie("fiber_flash")
1303+
}
1304+
1305+
func (c *DefaultCtx) GetFlash() error {
1306+
return c.SendString(c.flashMessages)
1307+
}

ctx_interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ type Ctx interface {
322322
// Replacement of: BodyParser, ParamsParser, GetReqHeaders, GetRespHeaders, AllParams, QueryParser, ReqHeaderParser
323323
Bind() *Bind
324324

325+
// setFlashes is a method to get flash messages before removing them
326+
setFlash()
327+
GetFlash() error
328+
325329
// SetReq resets fields of context that is relating to request.
326330
setReq(fctx *fasthttp.RequestCtx)
327331

@@ -425,6 +429,7 @@ func (c *DefaultCtx) release() {
425429
c.fasthttp = nil
426430
c.bind = nil
427431
c.redirect = nil
432+
c.flashMessages = ""
428433
if c.viewBindMap != nil {
429434
dictpool.ReleaseDict(c.viewBindMap)
430435
}

redirect.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
package fiber
66

77
import (
8+
"fmt"
9+
810
"github.com/valyala/bytebufferpool"
911
)
1012

1113
// Redirect is a struct to use it with Ctx.
1214
type Redirect struct {
13-
// Embed ctx
14-
c *DefaultCtx
15-
status int // Default: StatusFound
15+
c *DefaultCtx // Embed ctx
16+
status int // Status code of redirection. Default: StatusFound
17+
messages Map // Flash messages
1618
}
1719

1820
// A config to use with Redirect().Route()
@@ -26,8 +28,9 @@ type RedirectConfig struct {
2628
// Return default Redirect reference.
2729
func newRedirect(c *DefaultCtx) *Redirect {
2830
return &Redirect{
29-
c: c,
30-
status: StatusFound,
31+
c: c,
32+
status: StatusFound,
33+
messages: make(Map, 0),
3134
}
3235
}
3336

@@ -39,6 +42,14 @@ func (r *Redirect) Status(code int) *Redirect {
3942
return r
4043
}
4144

45+
// You can send flash messages by using With().
46+
// They will be sent as a cookie.
47+
func (r *Redirect) With(key string, value any) *Redirect {
48+
r.messages[key] = value
49+
50+
return r
51+
}
52+
4253
// Redirect to the URL derived from the specified path, with specified status.
4354
func (r *Redirect) To(location string) error {
4455
r.c.setCanonical(HeaderLocation, location)
@@ -62,6 +73,22 @@ func (r *Redirect) Route(name string, config ...RedirectConfig) error {
6273
return err
6374
}
6475

76+
// Flash messages
77+
if len(r.messages) > 0 {
78+
messageText := bytebufferpool.Get()
79+
defer bytebufferpool.Put(messageText)
80+
81+
for k, v := range r.messages {
82+
messageText.WriteString("k:" + k + ":" + fmt.Sprint(v) + ",")
83+
}
84+
85+
r.c.Cookie(&Cookie{
86+
Name: "fiber_flash",
87+
Value: messageText.String(),
88+
SessionOnly: true,
89+
})
90+
}
91+
6592
// Check queries
6693
if len(cfg.Queries) > 0 {
6794
queryText := bytebufferpool.Get()

router.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ func (app *App) handler(rctx *fasthttp.RequestCtx) {
160160
}
161161
c.Reset(rctx)
162162

163+
// check flash messages
164+
if c.Cookies("fiber_flash") != "" {
165+
c.setFlash()
166+
}
167+
163168
// handle invalid http method directly
164169
if methodInt(c.Method()) == -1 {
165170
_ = c.Status(StatusBadRequest).SendString("Invalid http method")

0 commit comments

Comments
 (0)