mirror of
https://github.com/eduardolat/pgbackweb.git
synced 2026-05-06 19:49:12 -05:00
Add session_id to reqctx
This commit is contained in:
@@ -26,7 +26,8 @@ func (m *Middleware) InjectReqctx(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
}
|
||||
|
||||
reqctx.SetCtx(c, reqctx.Ctx{
|
||||
IsAuthed: true,
|
||||
IsAuthed: true,
|
||||
SessionID: user.SessionID,
|
||||
User: dbgen.User{
|
||||
ID: user.ID,
|
||||
Name: user.Name,
|
||||
|
||||
@@ -2,41 +2,50 @@ package reqctx
|
||||
|
||||
import (
|
||||
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Context keys to avoid typos
|
||||
const (
|
||||
isAuthedKey = "isAuthed"
|
||||
userKey = "user"
|
||||
isAuthedKey = "isAuthed"
|
||||
sessionIDKey = "sessionId"
|
||||
userKey = "user"
|
||||
)
|
||||
|
||||
// Ctx represents the values passed through a single request context.
|
||||
type Ctx struct {
|
||||
IsAuthed bool
|
||||
User dbgen.User
|
||||
IsAuthed bool
|
||||
SessionID uuid.UUID
|
||||
User dbgen.User
|
||||
}
|
||||
|
||||
// SetCtx inserts values into the Echo request context.
|
||||
func SetCtx(c echo.Context, ctx Ctx) {
|
||||
c.Set(isAuthedKey, ctx.IsAuthed)
|
||||
c.Set(sessionIDKey, ctx.SessionID)
|
||||
c.Set(userKey, ctx.User)
|
||||
}
|
||||
|
||||
// GetCtx retrieves values from the Echo request context.
|
||||
func GetCtx(c echo.Context) Ctx {
|
||||
var isAuthed bool
|
||||
var sessionID uuid.UUID
|
||||
var user dbgen.User
|
||||
|
||||
if ia, ok := c.Get(isAuthedKey).(bool); ok {
|
||||
isAuthed = ia
|
||||
}
|
||||
if sid, ok := c.Get(sessionIDKey).(uuid.UUID); ok {
|
||||
sessionID = sid
|
||||
}
|
||||
if au, ok := c.Get(userKey).(dbgen.User); ok {
|
||||
user = au
|
||||
}
|
||||
|
||||
return Ctx{
|
||||
IsAuthed: isAuthed,
|
||||
User: user,
|
||||
IsAuthed: isAuthed,
|
||||
SessionID: sessionID,
|
||||
User: user,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ func TestCtxFuncs(t *testing.T) {
|
||||
Name: "John",
|
||||
}
|
||||
|
||||
testSessionID := uuid.New()
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
@@ -25,8 +27,9 @@ func TestCtxFuncs(t *testing.T) {
|
||||
|
||||
t.Run("Create authentication values in context", func(t *testing.T) {
|
||||
authData := Ctx{
|
||||
IsAuthed: true,
|
||||
User: testUser,
|
||||
IsAuthed: true,
|
||||
SessionID: testSessionID,
|
||||
User: testUser,
|
||||
}
|
||||
|
||||
SetCtx(c, authData)
|
||||
@@ -34,6 +37,7 @@ func TestCtxFuncs(t *testing.T) {
|
||||
|
||||
assert.True(t, auth.IsAuthed)
|
||||
assert.Equal(t, testUser, auth.User)
|
||||
assert.Equal(t, testSessionID, auth.SessionID)
|
||||
assert.Equal(t, testUser.Email, auth.User.Email)
|
||||
})
|
||||
|
||||
@@ -46,6 +50,7 @@ func TestCtxFuncs(t *testing.T) {
|
||||
auth := GetCtx(c)
|
||||
|
||||
assert.True(t, auth.IsAuthed)
|
||||
assert.Equal(t, uuid.Nil, auth.SessionID)
|
||||
assert.Empty(t, auth.User)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user