forked from mirrors/homebox
cleanup user implementation
This commit is contained in:
parent
9501eb398a
commit
a9f53a4671
6 changed files with 39 additions and 35 deletions
|
@ -15,17 +15,19 @@ type EntTokenRepository struct {
|
|||
|
||||
// GetUserFromToken get's a user from a token
|
||||
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.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 user
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue