A continuation of HomeBox the inventory and organization system built for the Home User
cleanup user implementation
Haydena9f53a4671a5
diff --git a/backend/app/api/base/base_ctrl.go b/backend/app/api/base/base_ctrl.goindex 9f41d06c..119967ee 100644--- a/backend/app/api/base/base_ctrl.go+++ b/backend/app/api/base/base_ctrl.go@@ -38,11 +38,6 @@ func (ctrl *BaseController) HandleBase(ready ReadyFunc, versions ...string) httpMessage: "Welcome to the Go API Template Application!",}- err := server.Respond(w, http.StatusOK, server.Wrap(data))-- if err != nil {- ctrl.log.Error(err, nil)- server.RespondInternalServerError(w)- }+ server.Respond(w, http.StatusOK, server.Wrap(data))}}diff --git a/backend/app/api/v1/v1_ctrl_auth.go b/backend/app/api/v1/v1_ctrl_auth.goindex 20f09f5b..4d265ea8 100644--- a/backend/app/api/v1/v1_ctrl_auth.go+++ b/backend/app/api/v1/v1_ctrl_auth.go@@ -10,11 +10,6 @@ import ("github.com/hay-kot/content/backend/pkgs/server")-var (- HeaderFormData = "application/x-www-form-urlencoded"- HeaderJSON = "application/json"-)-// HandleAuthLogin godoc// @Summary User Login// @Tags Authentication@@ -29,7 +24,7 @@ func (ctrl *V1Controller) HandleAuthLogin() http.HandlerFunc {return func(w http.ResponseWriter, r *http.Request) {loginForm := &types.LoginForm{}- if r.Header.Get("Content-Type") == HeaderFormData {+ if r.Header.Get("Content-Type") == server.ContentFormUrlEncoded {err := r.ParseForm()if err != nil {server.Respond(w, http.StatusBadRequest, server.Wrap(err))@@ -39,7 +34,7 @@ func (ctrl *V1Controller) HandleAuthLogin() http.HandlerFunc {loginForm.Username = r.PostFormValue("username")loginForm.Password = r.PostFormValue("password")- } else if r.Header.Get("Content-Type") == HeaderJSON {+ } else if r.Header.Get("Content-Type") == server.ContentJSON {err := server.Decode(r, loginForm)if err != nil {@@ -66,17 +61,10 @@ func (ctrl *V1Controller) HandleAuthLogin() http.HandlerFunc {return}- err = server.Respond(w, http.StatusOK, types.TokenResponse{+ server.Respond(w, http.StatusOK, types.TokenResponse{BearerToken: "Bearer " + newToken.Raw,ExpiresAt: newToken.ExpiresAt,})-- if err != nil {- ctrl.log.Error(err, logger.Props{- "user": loginForm.Username,- })- return- }}}@@ -130,10 +118,6 @@ func (ctrl *V1Controller) HandleAuthRefresh() http.HandlerFunc {return}- err = server.Respond(w, http.StatusOK, newToken)-- if err != nil {- return- }+ server.Respond(w, http.StatusOK, newToken)}}diff --git a/backend/app/api/v1/v1_ctrl_user.go b/backend/app/api/v1/v1_ctrl_user.goindex a3371c22..ca293783 100644--- a/backend/app/api/v1/v1_ctrl_user.go+++ b/backend/app/api/v1/v1_ctrl_user.go@@ -28,14 +28,14 @@ func (ctrl *V1Controller) HandleUserRegistration() http.HandlerFunc {return}- usr, err := ctrl.svc.User.RegisterUser(r.Context(), regData)+ _, err := ctrl.svc.User.RegisterUser(r.Context(), regData)if err != nil {ctrl.log.Error(err, nil)server.RespondError(w, http.StatusInternalServerError, err)return}- _ = server.Respond(w, http.StatusOK, server.Wrap(usr))+ server.Respond(w, http.StatusNoContent, nil)}}@@ -52,11 +52,11 @@ func (ctrl *V1Controller) HandleUserSelf() http.HandlerFunc {usr, err := ctrl.svc.User.GetSelf(r.Context(), token)if usr.ID == uuid.Nil || err != nil {ctrl.log.Error(errors.New("no user within request context"), nil)- server.RespondInternalServerError(w)+ server.RespondServerError(w)return}- _ = server.Respond(w, http.StatusOK, server.Wrap(usr))+ server.Respond(w, http.StatusOK, server.Wrap(usr))}}@@ -92,7 +92,7 @@ func (ctrl *V1Controller) HandleUserUpdate() http.HandlerFunc {return}- _ = server.Respond(w, http.StatusOK, server.Wrap(newData))+ server.Respond(w, http.StatusOK, server.Wrap(newData))}}diff --git a/backend/internal/repo/repo_tokens.go b/backend/internal/repo/repo_tokens.goindex a1e292cf..d40360f2 100644--- a/backend/internal/repo/repo_tokens.go+++ b/backend/internal/repo/repo_tokens.go@@ -15,17 +15,19 @@ type EntTokenRepository struct {// GetUserFromToken get's a user from a tokenfunc (r *EntTokenRepository) GetUserFromToken(ctx context.Context, token []byte) (*ent.User, error) {- dbToken, err := r.db.AuthTokens.Query().+ user, err := r.db.AuthTokens.Query().Where(authtokens.Token(token)).Where(authtokens.ExpiresAtGTE(time.Now())).WithUser().+ QueryUser().+ WithGroup().Only(ctx)if err != nil {return nil, err}- return dbToken.Edges.User, nil+ return user, nil}// Creates a token for a userdiff --git a/backend/internal/services/service_user.go b/backend/internal/services/service_user.goindex de1cb27b..3264d727 100644--- a/backend/internal/services/service_user.go+++ b/backend/internal/services/service_user.go@@ -23,6 +23,20 @@ type UserService struct {repos *repo.AllRepos}+func (UserService) toOutUser(user *ent.User, err error) (types.UserOut, error) {+ if err != nil {+ return types.UserOut{}, err+ }+ return types.UserOut{+ ID: user.ID,+ Name: user.Name,+ Email: user.Email,+ IsSuperuser: user.IsSuperuser,+ GroupName: user.Edges.Group.Name,+ GroupID: user.Edges.Group.ID,+ }, nil+}+func (svc *UserService) RegisterUser(ctx context.Context, data types.UserRegistration) (*ent.User, error) {group, err := svc.repos.Groups.Create(ctx, data.GroupName)if err != nil {@@ -48,9 +62,9 @@ func (svc *UserService) RegisterUser(ctx context.Context, data types.UserRegistr}// GetSelf returns the user that is currently logged in based of the token provided within-func (svc *UserService) GetSelf(ctx context.Context, requestToken string) (*ent.User, error) {+func (svc *UserService) GetSelf(ctx context.Context, requestToken string) (types.UserOut, error) {hash := hasher.HashToken(requestToken)- return svc.repos.AuthTokens.GetUserFromToken(ctx, hash)+ return svc.toOutUser(svc.repos.AuthTokens.GetUserFromToken(ctx, hash))}func (svc *UserService) UpdateSelf(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (*ent.User, error) {diff --git a/backend/internal/types/users_types.go b/backend/internal/types/users_types.goindex 81cc932b..61a1cc1a 100644--- a/backend/internal/types/users_types.go+++ b/backend/internal/types/users_types.go@@ -49,3 +49,12 @@ type UserRegistration struct {User UserIn `json:"user"`GroupName string `json:"groupName"`}++type UserOut struct {+ ID uuid.UUID `json:"id"`+ Name string `json:"name"`+ Email string `json:"email"`+ IsSuperuser bool `json:"isSuper"`+ GroupID uuid.UUID `json:"groupId"`+ GroupName string `json:"groupName"`+}