feat: add /users/me route

This commit is contained in:
2025-12-04 00:49:59 +00:00
parent 539b15dcb7
commit 1bedafd3de
3 changed files with 34 additions and 2 deletions

View File

@@ -98,6 +98,7 @@ func NewServer(c Config) (*Server, error) {
api := app.Group("/api") api := app.Group("/api")
auth.NewHTTPHandler(authService, db, cookieConfig).RegisterRoutes(api) auth.NewHTTPHandler(authService, db, cookieConfig).RegisterRoutes(api)
user.NewHTTPHandler(userService, db, authMiddleware).RegisterRoutes(api)
accountRouter := account.NewHTTPHandler(accountService, authService, db, authMiddleware).RegisterRoutes(api) accountRouter := account.NewHTTPHandler(accountService, authService, db, authMiddleware).RegisterRoutes(api)
upload.NewHTTPHandler(uploadService, db).RegisterRoutes(accountRouter) upload.NewHTTPHandler(uploadService, db).RegisterRoutes(accountRouter)

View File

@@ -0,0 +1,31 @@
package user
import (
"github.com/get-drexa/drexa/internal/reqctx"
"github.com/gofiber/fiber/v2"
"github.com/uptrace/bun"
)
type HTTPHandler struct {
service *Service
db *bun.DB
authMiddleware fiber.Handler
}
func NewHTTPHandler(service *Service, db *bun.DB, authMiddleware fiber.Handler) *HTTPHandler {
return &HTTPHandler{service: service, db: db, authMiddleware: authMiddleware}
}
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) {
user := api.Group("/users")
user.Use(h.authMiddleware)
user.Get("/me", h.getAuthenticatedUser)
}
func (h *HTTPHandler) getAuthenticatedUser(c *fiber.Ctx) error {
u := reqctx.AuthenticatedUser(c).(*User)
if u == nil {
return c.SendStatus(fiber.StatusUnauthorized)
}
return c.JSON(u)
}

View File

@@ -15,8 +15,8 @@ type User struct {
DisplayName string `bun:"display_name" json:"displayName"` DisplayName string `bun:"display_name" json:"displayName"`
Email string `bun:"email,unique,notnull" json:"email"` Email string `bun:"email,unique,notnull" json:"email"`
Password password.Hashed `bun:"password,notnull" json:"-"` Password password.Hashed `bun:"password,notnull" json:"-"`
CreatedAt time.Time `bun:"created_at,notnull,nullzero" json:"createdAt"` CreatedAt time.Time `bun:"created_at,notnull,nullzero" json:"-"`
UpdatedAt time.Time `bun:"updated_at,notnull,nullzero" json:"updatedAt"` UpdatedAt time.Time `bun:"updated_at,notnull,nullzero" json:"-"`
} }
func newUserID() (uuid.UUID, error) { func newUserID() (uuid.UUID, error) {