Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues/169: Set session cookie only if non-empty #170

Merged
merged 3 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)
}