auth/internal/server/handler_dashboard.go
Ruidy d642716fd0
refactor(auth): move auth package to service directory
Relocated internal/auth to internal/service/auth for improved project
structure
and separation of concerns. Updated all imports to reflect new paths.
Split
UserStore interface and error definitions into a dedicated store.go
file.
Moved generateUserID to user.go for better encapsulation.
2025-09-20 13:04:13 +02:00

42 lines
1.1 KiB
Go

package server
import (
"log"
"net/http"
"time"
"github.com/rjnemo/auth/internal/service/auth"
)
const dashboardTimeDisplayLayout = "02 Jan 2006 15:04 MST"
func (s *Server) dashboardHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
state := sessionFromContext(r.Context())
if !state.Authenticated {
w.WriteHeader(http.StatusUnauthorized)
s.render(w, "unauthorized.html", newUnauthorizedData("Sign in to continue.", state.CSRFToken))
return
}
email, err := auth.NewUserEmail(state.Email)
if err != nil {
log.Printf("dashboard: invalid session email: %v", err)
http.Error(w, "session invalid", http.StatusUnauthorized)
return
}
account, err := s.authService.LookupByEmail(r.Context(), email)
if err != nil {
log.Printf("dashboard: lookup failed: %v", err)
http.Error(w, "unable to load account", http.StatusInternalServerError)
return
}
createdAtISO := account.CreatedAt.Format(time.RFC3339)
createdAtDisplay := account.CreatedAt.Format(dashboardTimeDisplayLayout)
s.render(w, "in.html", newDashboardData(state.Email, state.CSRFToken, createdAtDisplay, createdAtISO))
}
}