Skip to content

Commit

Permalink
Поправил выставление user ID при регистрации
Browse files Browse the repository at this point in the history
  • Loading branch information
Froctnow committed May 19, 2024
1 parent 180782e commit 66e6505
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions internal/app/httpserver/user/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *userRouter) Register(ctx *gin.Context) {
return
}

err := r.authUseCase.Register(ctx, req.Login, req.Password)
userID, err := r.authUseCase.Register(ctx, req.Login, req.Password)

if err != nil && errors.As(err, &autherrors.UserAlreadyExistsError{}) {
ctx.AbortWithStatus(http.StatusConflict)
Expand All @@ -46,7 +46,7 @@ func (r *userRouter) Register(ctx *gin.Context) {
return
}

err = r.authorizeUser(ctx, req.Login)
err = r.authorizeUser(ctx, userID)

if err != nil {
r.logger.ErrorCtx(ctx, fmt.Errorf("failed authorize user"))
Expand Down
10 changes: 5 additions & 5 deletions internal/app/usecase/auth/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import (
"github.com/Froctnow/yandex-go-diploma/internal/app/usecase/auth/errors"
)

func (u *authUseCase) Register(ctx context.Context, login string, password string) error {
func (u *authUseCase) Register(ctx context.Context, login string, password string) (string, error) {
hashPassword, err := u.hashPassword(password)

if err != nil {
err = fmt.Errorf("can't hash password: %w", err)
u.logger.ErrorCtx(ctx, err)
return err
return "", err
}

userID, err := u.provider.CreateUser(ctx, nil, login, hashPassword)

if err != nil {
err = fmt.Errorf("can't create user: %w", err)
u.logger.ErrorCtx(ctx, err)
return err
return "", err
}

if userID == "" {
err = errors.UserAlreadyExistsError{}
u.logger.ErrorCtx(ctx, err)
return err
return "", err
}

return nil
return userID, nil
}
2 changes: 1 addition & 1 deletion internal/app/usecase/auth/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewUseCase(
}

type UseCase interface {
Register(ctx context.Context, login string, password string) error
Register(ctx context.Context, login string, password string) (string, error)
Login(ctx context.Context, login string, password string) (string, error)
}

Expand Down

0 comments on commit 66e6505

Please sign in to comment.