Skip to content

Commit

Permalink
Use a constant for the session key name
Browse files Browse the repository at this point in the history
We're using a 'magic string' for the echo session key name, which makes it easy for the different instances of the string to go out of sync. Using a named constant makes the intent clear and ensures all copies of the key name in the code stay in sync.
  • Loading branch information
mtlynch committed Dec 27, 2024
1 parent 6f3ac27 commit 64b071b
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

type Session struct{}

const sessionKeyName = "fusion-client-session"

func (s Session) Create(c echo.Context) error {
var req struct {
Password string `json:"password" validate:"required"`
Expand All @@ -24,7 +26,7 @@ func (s Session) Create(c echo.Context) error {
return echo.NewHTTPError(http.StatusUnauthorized, "Wrong password")
}

sess, _ := session.Get("login", c)
sess, _ := session.Get(sessionKeyName, c)

if !conf.Conf.SecureCookie {
sess.Options.Secure = false
Expand All @@ -40,7 +42,7 @@ func (s Session) Create(c echo.Context) error {
}

func (s Session) Check(c echo.Context) (bool, error) {
sess, err := session.Get("login", c)
sess, err := session.Get(sessionKeyName, c)
if err != nil {
return false, err
}
Expand All @@ -52,7 +54,7 @@ func (s Session) Check(c echo.Context) (bool, error) {
}

func (s Session) Delete(c echo.Context) error {
sess, err := session.Get("login", c)
sess, err := session.Get(sessionKeyName, c)
if err != nil {
return err
}
Expand Down

0 comments on commit 64b071b

Please sign in to comment.