summaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-05-28 14:26:19 -0600
committermo khan <mo@mokhan.ca>2025-05-28 14:26:19 -0600
commite9546b40c8befabda26c1598c124a6ee2a8d2b8f (patch)
treec7b09c0c1c821b516e56b5ac3637dc07dc97d039 /app/controllers
parent1de6a34a55c2e8b7d50945984acb45e7809f6a37 (diff)
refactor: always provide a user in the request context
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/dashboard/controller.go6
-rw-r--r--app/controllers/dashboard/controller_test.go3
-rw-r--r--app/controllers/sparkles/controller_test.go3
3 files changed, 6 insertions, 6 deletions
diff --git a/app/controllers/dashboard/controller.go b/app/controllers/dashboard/controller.go
index 04a7ed1..d279930 100644
--- a/app/controllers/dashboard/controller.go
+++ b/app/controllers/dashboard/controller.go
@@ -41,14 +41,12 @@ func (c *Controller) Show(w http.ResponseWriter, r *http.Request) {
}
func (c *Controller) Navigation(w http.ResponseWriter, r *http.Request) {
- currentUser := cfg.CurrentUser.From(r.Context())
-
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "text/html")
dto := &NavigationDTO{
- CurrentUser: currentUser,
- IsLoggedIn: currentUser != nil,
+ CurrentUser: cfg.CurrentUser.From(r.Context()),
+ IsLoggedIn: middleware.IsLoggedIn(r),
}
if err := views.Render(w, "dashboard/nav", dto); err != nil {
pls.LogError(r.Context(), err)
diff --git a/app/controllers/dashboard/controller_test.go b/app/controllers/dashboard/controller_test.go
index c717a74..ddbfd34 100644
--- a/app/controllers/dashboard/controller_test.go
+++ b/app/controllers/dashboard/controller_test.go
@@ -28,7 +28,7 @@ func TestController(t *testing.T) {
})
t.Run("when authenticated", func(t *testing.T) {
- ctx := cfg.CurrentUser.With(t.Context(), &domain.User{})
+ ctx := cfg.CurrentUser.With(t.Context(), &domain.User{ID: domain.ID("1")})
r, w := test.RequestResponse("GET", "/dashboard", test.WithContext(ctx))
mux.ServeHTTP(w, r)
@@ -55,6 +55,7 @@ func TestController(t *testing.T) {
t.Run("when authenticated", func(t *testing.T) {
ctx := cfg.CurrentUser.With(t.Context(), &domain.User{
+ ID: domain.ID("1"),
Username: "root",
})
r, w := test.RequestResponse("GET", "/dashboard/nav", test.WithContext(ctx))
diff --git a/app/controllers/sparkles/controller_test.go b/app/controllers/sparkles/controller_test.go
index 8a1717d..0619b99 100644
--- a/app/controllers/sparkles/controller_test.go
+++ b/app/controllers/sparkles/controller_test.go
@@ -44,9 +44,10 @@ func TestSparkles(t *testing.T) {
t.Run("POST /sparkles", func(t *testing.T) {
t.Run("when a user is logged in", func(t *testing.T) {
- currentUser := &domain.User{}
+ currentUser := domain.NewUser(domain.WithID[*domain.User](domain.ID("1")))
t.Run("saves a new sparkle", func(t *testing.T) {
+
repository := db.NewRepository[*domain.Sparkle]()
mux := http.NewServeMux()
controller := New(repository)