Skip to content

Commit

Permalink
change password bug terminated
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeMaster482 committed Dec 14, 2023
1 parent d9c0b1e commit 6eb1545
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 77 deletions.
5 changes: 4 additions & 1 deletion cmd/api/init/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ func InitRouter(auth *auth.Handler,
authRouter.Methods("POST").Path("/checkAuth").HandlerFunc(auth.HealthCheck)
authRouter.Methods("POST").Path("/loginCheck").HandlerFunc(auth.CheckLoginUnique)
authRouter.Methods("POST").Path("/logout").HandlerFunc(auth.LogOut)
authRouter.Methods("PUT").Path("/password").HandlerFunc(auth.ChangePassword)
// TODO:Костыль наверно в юзера надо
passwordRouter := authRouter.PathPrefix("/password").Subrouter()
passwordRouter.Use(authMid.Authentication)
passwordRouter.Methods("PUT").Path("/").HandlerFunc(auth.ChangePassword)
}

accountRouter := apiRouter.PathPrefix("/account").Subrouter()
Expand Down
2 changes: 1 addition & 1 deletion internal/microservices/auth/auth_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type (
}

ChangePasswordInput struct {
Id uuid.UUID
Login string
OldPassword string `json:"old_password" valid:"required,length(4|20)"`
NewPassword string `json:"new_password" valid:"required,length(4|20)"`
}
Expand Down
132 changes: 66 additions & 66 deletions internal/microservices/auth/delivery/grpc/generated/auth.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions internal/microservices/auth/delivery/grpc/grpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)

type authGRPC struct {
Expand Down Expand Up @@ -123,6 +124,19 @@ func (a *authGRPC) GetByID(ctx context.Context, in *proto.UserIdRequest) (*proto
}, nil
}

func (a *authGRPC) ChangePassword(ctx context.Context, in *proto.ChangePasswordRequest) (*emptypb.Empty, error) {
err := a.authServices.ChangePassword(ctx, auth.ChangePasswordInput{
Login: in.Login,
OldPassword: in.OldPassword,
NewPassword: in.NewPassword,
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &emptypb.Empty{}, nil
}

// func (a *authGRPC) HealthCheck(ctx context.Context, in *emptypb.Empty) (*proto.HelthCheckResponse, error) {
// /// a.sessionService.GetSessionByCookie(ctx)
// return nil, status.Errorf(codes.Unimplemented, "method HealthCheck not implemented")
Expand Down
10 changes: 5 additions & 5 deletions internal/microservices/auth/delivery/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,13 @@ func (h *Handler) GetByIdHandler(w http.ResponseWriter, r *http.Request) {
// @Summary Change Password
// @Tags Auth
// @Description Takes old password and newpassword and chnge password
// @Accept json
// @Produce json
// @Success 200 {object} Response[auth.ChangePasswordInput] "password Info"
// @Param userInput body auth.ChangePasswordInput true "username && password"
// @Success 200 {object} Response[auth.SignResponse] "user Info"
// @Failure 400 {object} ResponseError "Client error"
// @Failure 500 {object} ResponseError "Server error"
// @Router /api/auth/password [put]
// @Router /api/auth/password/ [put]
func (h *Handler) ChangePassword(w http.ResponseWriter, r *http.Request) {
user, err := response.GetUserFromRequest(r)
if err != nil {
Expand Down Expand Up @@ -390,11 +392,9 @@ func (h *Handler) ChangePassword(w http.ResponseWriter, r *http.Request) {
return
}

changePassword.Id = user.ID

// Change Password
_, err = h.client.ChangePassword(r.Context(), &gen.ChangePasswordRequest{
Id: changePassword.Id.String(),
Login: user.Login,
OldPassword: changePassword.OldPassword,
NewPassword: changePassword.NewPassword,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
UserCheckLoginUnique = `SELECT COUNT(*) FROM users WHERE login = $1;`
UserGetByUserName = `SELECT id, login, username, password_hash, planned_budget, avatar_url From users WHERE (login=$1);`
UserCreate = `INSERT INTO users (login, username, password_hash) VALUES ($1, $2, $3) RETURNING id;`
UserIDGetByID = `SELECT id, login, username, password_hash, planned_budget, avatar_url FROM users WHERE id = $1;`
UserIDGetByID = `SELECT id, login, username, password_hash, planned_budget, avatar_url FROM users WHERE id=CAST($1 AS UUID);`
UserChangePassword = `UPDATE users SET password_hash = $1 WHERE id = $2;`
)

Expand Down
4 changes: 2 additions & 2 deletions internal/microservices/auth/usecase/auth_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (u *Usecase) GetByID(ctx context.Context, userID uuid.UUID) (*models.User,
}

func (u *Usecase) ChangePassword(ctx context.Context, input auth.ChangePasswordInput) error {
user, err := u.authRepo.GetByID(ctx, input.Id)
user, err := u.authRepo.GetUserByLogin(ctx, input.Login)
if err != nil {
return fmt.Errorf("[usecase] can't find user: %w", err)
}
Expand All @@ -112,7 +112,7 @@ func (u *Usecase) ChangePassword(ctx context.Context, input auth.ChangePasswordI
return fmt.Errorf("[usecase] can't change password intenal err: %w", err)
}

if err := u.authRepo.ChangePassword(ctx, input.Id, newpwd); err != nil {
if err := u.authRepo.ChangePassword(ctx, user.ID, newpwd); err != nil {
return fmt.Errorf("[usecase] can't change password %w", err)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion proto/auth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ message UserIdRequest {
};

message ChangePasswordRequest {
string id = 1;
string login = 1;
string oldPassword = 2;
string newPassword = 3;
};
Expand Down

0 comments on commit 6eb1545

Please sign in to comment.