Skip to content

Commit

Permalink
issues/169: Set session cookie only if non-empty (#170)
Browse files Browse the repository at this point in the history
What:
- Set session cookie only if non-empty
- I was unable to add a testcase, but I did test manually and it seems okay

Why:
- Fixes: #169
  • Loading branch information
komuw authored Oct 26, 2022
1 parent 2306e72 commit 91e474d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Most recent version is listed first.


## v0.0.24
- Set session cookie only if non-empty: https://github.com/komuw/ong/pull/170

## v0.0.23
- ong/client: Add log id http header: https://github.com/komuw/ong/pull/166

Expand Down
35 changes: 20 additions & 15 deletions sess/sess.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,25 @@ func Save(
mAge time.Duration,
secretKey string,
) {
ctx := r.Context()
if vCtx := ctx.Value(ctxKey); vCtx != nil {
if s, ok := vCtx.(M); ok {
if value, err := json.Marshal(s); err == nil && value != nil {
cookie.SetEncrypted(
r,
w,
CookieName,
string(value),
domain,
mAge,
secretKey,
)
}
}
savedSess := GetM(r)
if len(savedSess) <= 0 {
// If GetM returns a zero-length map, then we do not have to write any session.
return
}

value, err := json.Marshal(savedSess)
if err != nil || value == nil {
// Technically, err can never be non-nil and value can never be nil.
// This is because, at this point; we know for sure that savedSess is a non zero-length map[string]string
return
}
cookie.SetEncrypted(
r,
w,
CookieName,
string(value),
domain,
mAge,
secretKey,
)
}

0 comments on commit 91e474d

Please sign in to comment.