diff --git a/controller/page.go b/controller/page.go index 2877b4c..0922f71 100644 --- a/controller/page.go +++ b/controller/page.go @@ -6,6 +6,7 @@ import ( "time" "goweb/context" + "goweb/ent" "goweb/htmx" "goweb/msg" @@ -68,6 +69,9 @@ type Page struct { // IsAuth stores whether or not the user is authenticated IsAuth bool + // AuthUser stores the authenticated user + AuthUser *ent.User + // StatusCode stores the HTTP status code that will be returned StatusCode int @@ -137,6 +141,7 @@ func NewPage(ctx echo.Context) Page { if u := ctx.Get(context.AuthenticatedUserKey); u != nil { p.IsAuth = true + p.AuthUser = u.(*ent.User) } p.HTMX.Request = htmx.GetRequest(ctx) diff --git a/controller/page_test.go b/controller/page_test.go index e45bbe6..b07620f 100644 --- a/controller/page_test.go +++ b/controller/page_test.go @@ -10,6 +10,7 @@ import ( echomw "github.com/labstack/echo/v4/middleware" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNewPage(t *testing.T) { @@ -29,13 +30,16 @@ func TestNewPage(t *testing.T) { assert.False(t, p.Cache.Enabled) ctx, _ = tests.NewContext(c.Web, "/abc?def=123") - ctx.Set(context.AuthenticatedUserKey, 1) + usr, err := tests.CreateUser(c.ORM) + require.NoError(t, err) + ctx.Set(context.AuthenticatedUserKey, usr) ctx.Set(echomw.DefaultCSRFConfig.ContextKey, "csrf") p = NewPage(ctx) assert.Equal(t, "/abc", p.Path) assert.Equal(t, "/abc?def=123", p.URL) assert.False(t, p.IsHome) assert.True(t, p.IsAuth) + assert.Equal(t, usr, p.AuthUser) assert.Equal(t, "csrf", p.CSRF) } diff --git a/ent/schema/user.go b/ent/schema/user.go index 0938267..9d2a79b 100644 --- a/ent/schema/user.go +++ b/ent/schema/user.go @@ -43,6 +43,7 @@ func (User) Edges() []ent.Edge { } } +// Hooks of the User. func (User) Hooks() []ent.Hook { return []ent.Hook{ hook.On( diff --git a/templates/layouts/main.gohtml b/templates/layouts/main.gohtml index 637cb1d..8ad3a7a 100644 --- a/templates/layouts/main.gohtml +++ b/templates/layouts/main.gohtml @@ -13,31 +13,48 @@
-