chore: update get user by id

This commit is contained in:
boojack 2022-07-09 08:31:07 +08:00
parent 1afc183458
commit ac560dfcf9
2 changed files with 7 additions and 10 deletions

View file

@ -55,7 +55,7 @@ func removeUserSession(c echo.Context) error {
func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc { func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
// Skip auth for some paths. // 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) return next(c)
} }
@ -76,9 +76,11 @@ 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 { 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) return next(c)
} }
}
sess, err := session.Get("session", c) sess, err := session.Get("session", c)
if err != nil { if err != nil {

View file

@ -51,7 +51,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return nil 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")) id, err := strconv.Atoi(c.Param("id"))
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted user id").SetInternal(err) 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) 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) 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 echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
} }
return nil return nil