cleanup user implementation

This commit is contained in:
Hayden 2022-08-30 16:40:39 -08:00
parent 9501eb398a
commit a9f53a4671
6 changed files with 39 additions and 35 deletions

View file

@ -38,11 +38,6 @@ func (ctrl *BaseController) HandleBase(ready ReadyFunc, versions ...string) http
Message: "Welcome to the Go API Template Application!", Message: "Welcome to the Go API Template Application!",
} }
err := server.Respond(w, http.StatusOK, server.Wrap(data)) server.Respond(w, http.StatusOK, server.Wrap(data))
if err != nil {
ctrl.log.Error(err, nil)
server.RespondInternalServerError(w)
}
} }
} }

View file

@ -10,11 +10,6 @@ import (
"github.com/hay-kot/content/backend/pkgs/server" "github.com/hay-kot/content/backend/pkgs/server"
) )
var (
HeaderFormData = "application/x-www-form-urlencoded"
HeaderJSON = "application/json"
)
// HandleAuthLogin godoc // HandleAuthLogin godoc
// @Summary User Login // @Summary User Login
// @Tags Authentication // @Tags Authentication
@ -29,7 +24,7 @@ func (ctrl *V1Controller) HandleAuthLogin() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
loginForm := &types.LoginForm{} loginForm := &types.LoginForm{}
if r.Header.Get("Content-Type") == HeaderFormData { if r.Header.Get("Content-Type") == server.ContentFormUrlEncoded {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
server.Respond(w, http.StatusBadRequest, server.Wrap(err)) server.Respond(w, http.StatusBadRequest, server.Wrap(err))
@ -39,7 +34,7 @@ func (ctrl *V1Controller) HandleAuthLogin() http.HandlerFunc {
loginForm.Username = r.PostFormValue("username") loginForm.Username = r.PostFormValue("username")
loginForm.Password = r.PostFormValue("password") 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) err := server.Decode(r, loginForm)
if err != nil { if err != nil {
@ -66,17 +61,10 @@ func (ctrl *V1Controller) HandleAuthLogin() http.HandlerFunc {
return return
} }
err = server.Respond(w, http.StatusOK, types.TokenResponse{ server.Respond(w, http.StatusOK, types.TokenResponse{
BearerToken: "Bearer " + newToken.Raw, BearerToken: "Bearer " + newToken.Raw,
ExpiresAt: newToken.ExpiresAt, 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 return
} }
err = server.Respond(w, http.StatusOK, newToken) server.Respond(w, http.StatusOK, newToken)
if err != nil {
return
}
} }
} }

View file

@ -28,14 +28,14 @@ func (ctrl *V1Controller) HandleUserRegistration() http.HandlerFunc {
return return
} }
usr, err := ctrl.svc.User.RegisterUser(r.Context(), regData) _, err := ctrl.svc.User.RegisterUser(r.Context(), regData)
if err != nil { if err != nil {
ctrl.log.Error(err, nil) ctrl.log.Error(err, nil)
server.RespondError(w, http.StatusInternalServerError, err) server.RespondError(w, http.StatusInternalServerError, err)
return 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) usr, err := ctrl.svc.User.GetSelf(r.Context(), token)
if usr.ID == uuid.Nil || err != nil { if usr.ID == uuid.Nil || err != nil {
ctrl.log.Error(errors.New("no user within request context"), nil) ctrl.log.Error(errors.New("no user within request context"), nil)
server.RespondInternalServerError(w) server.RespondServerError(w)
return 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 return
} }
_ = server.Respond(w, http.StatusOK, server.Wrap(newData)) server.Respond(w, http.StatusOK, server.Wrap(newData))
} }
} }

View file

@ -15,17 +15,19 @@ type EntTokenRepository struct {
// GetUserFromToken get's a user from a token // GetUserFromToken get's a user from a token
func (r *EntTokenRepository) GetUserFromToken(ctx context.Context, token []byte) (*ent.User, error) { func (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.Token(token)).
Where(authtokens.ExpiresAtGTE(time.Now())). Where(authtokens.ExpiresAtGTE(time.Now())).
WithUser(). WithUser().
QueryUser().
WithGroup().
Only(ctx) Only(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return dbToken.Edges.User, nil return user, nil
} }
// Creates a token for a user // Creates a token for a user

View file

@ -23,6 +23,20 @@ type UserService struct {
repos *repo.AllRepos 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) { func (svc *UserService) RegisterUser(ctx context.Context, data types.UserRegistration) (*ent.User, error) {
group, err := svc.repos.Groups.Create(ctx, data.GroupName) group, err := svc.repos.Groups.Create(ctx, data.GroupName)
if err != nil { 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 // 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) 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) { func (svc *UserService) UpdateSelf(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (*ent.User, error) {

View file

@ -49,3 +49,12 @@ type UserRegistration struct {
User UserIn `json:"user"` User UserIn `json:"user"`
GroupName string `json:"groupName"` 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"`
}