From ac560dfcf98cf04d6978067bf26d0f8a7139ca5d Mon Sep 17 00:00:00 2001 From: boojack Date: Sat, 9 Jul 2022 08:31:07 +0800 Subject: [PATCH] chore: update get user by id --- server/basic_auth.go | 8 +++++--- server/user.go | 9 ++------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/server/basic_auth.go b/server/basic_auth.go index 717ae815..a3925aa2 100644 --- a/server/basic_auth.go +++ b/server/basic_auth.go @@ -55,7 +55,7 @@ func removeUserSession(c echo.Context) error { func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { // Skip auth for some paths. - if common.HasPrefixes(c.Path(), "/api/auth", "/api/ping", "/api/status") { + if common.HasPrefixes(c.Path(), "/api/auth", "/api/ping", "/api/status", "/api/user/:id/") { return next(c) } @@ -76,8 +76,10 @@ func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc { } } - if common.HasPrefixes(c.Path(), "/api/memo", "/api/tag", "/api/shortcut", "/api/user/:id/name") && c.Request().Method == http.MethodGet { - return next(c) + if common.HasPrefixes(c.Path(), "/api/memo", "/api/tag", "/api/shortcut") && c.Request().Method == http.MethodGet { + if _, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil { + return next(c) + } } sess, err := session.Get("session", c) diff --git a/server/user.go b/server/user.go index f19bb6f5..94c522cb 100644 --- a/server/user.go +++ b/server/user.go @@ -51,7 +51,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) { return nil }) - g.GET("/user/:id/name", func(c echo.Context) error { + g.GET("/user/:id", func(c echo.Context) error { id, err := strconv.Atoi(c.Param("id")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, "Malformatted user id").SetInternal(err) @@ -64,13 +64,8 @@ func (s *Server) registerUserRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch user").SetInternal(err) } - username := "" - if user != nil { - username = user.Name - } - c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) - if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(username)); err != nil { + if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err) } return nil