-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feat/single-product
Signed-off-by: Michael <[email protected]>
- Loading branch information
Showing
14 changed files
with
373 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package auth | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/internal/models" | ||
service "github.com/hngprojects/hng_boilerplate_golang_web/services/auth" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/utility" | ||
) | ||
|
||
func (base *Controller) ChangePassword(c *gin.Context) { | ||
var ( | ||
req = models.ChangePasswordRequestModel{} | ||
) | ||
|
||
err := c.ShouldBind(&req) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", "Failed to parse request body", err, nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
err = base.Validator.Struct(&req) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusUnprocessableEntity, "error", "Validation failed", | ||
utility.ValidationResponse(err, base.Validator), nil) | ||
c.JSON(http.StatusUnprocessableEntity, rd) | ||
return | ||
} | ||
|
||
respData, code, err := service.UpdateUserPassword(c, req, base.Db.Postgresql) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(code, "error", err.Error(), err, nil) | ||
c.JSON(code, rd) | ||
return | ||
} | ||
|
||
base.Logger.Info("password changed successfully") | ||
|
||
rd := utility.BuildSuccessResponse(http.StatusOK, "Password updated successfully", respData) | ||
c.JSON(http.StatusOK, rd) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/internal/models" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/middleware" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/utility" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func UpdateUserPassword(c *gin.Context, req models.ChangePasswordRequestModel, db *gorm.DB) (*models.User, int, error) { | ||
|
||
user := models.User{} | ||
|
||
userId, err := middleware.GetUserClaims(c, db, "user_id") | ||
if err != nil { | ||
return nil, http.StatusNotFound, err | ||
} | ||
|
||
userID, ok := userId.(string) | ||
if !ok { | ||
return nil, http.StatusBadRequest, errors.New("user_id is not of type string") | ||
} | ||
|
||
userDataExist, err := user.GetUserByID(db, userID) | ||
if err != nil { | ||
return nil, http.StatusNotFound, fmt.Errorf("unable to fetch user " + err.Error()) | ||
} | ||
|
||
if !utility.CompareHash(req.OldPassword, userDataExist.Password) { | ||
return nil, http.StatusBadRequest, fmt.Errorf("old password is incorrect") | ||
} | ||
|
||
if req.OldPassword == req.NewPassword { | ||
return nil, http.StatusConflict, errors.New("new password cannot be the same as the old password") | ||
} | ||
|
||
hashedPassword, err := utility.HashPassword(req.NewPassword) | ||
if err != nil { | ||
return nil, http.StatusBadRequest, err | ||
} | ||
|
||
userDataExist.Password = hashedPassword | ||
err = userDataExist.Update(db) | ||
if err != nil { | ||
return nil, http.StatusBadRequest, err | ||
} | ||
|
||
return &userDataExist, http.StatusOK, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package test_auth | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/go-playground/validator/v10" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/internal/models" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/controller/auth" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/middleware" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/tests" | ||
) | ||
|
||
func SetupAuthTestRouter() (*gin.Engine, *auth.Controller) { | ||
gin.SetMode(gin.TestMode) | ||
|
||
logger := tests.Setup() | ||
db := storage.Connection() | ||
validator := validator.New() | ||
|
||
authController := &auth.Controller{ | ||
Db: db, | ||
Validator: validator, | ||
Logger: logger, | ||
} | ||
|
||
r := gin.Default() | ||
SetupAuthRoutes(r, authController) | ||
return r, authController | ||
} | ||
|
||
func SetupAuthRoutes(r *gin.Engine, userController *auth.Controller) { | ||
r.PUT("/api/v1/auth/change-password", | ||
middleware.Authorize(userController.Db.Postgresql, models.RoleIdentity.SuperAdmin, models.RoleIdentity.User), | ||
userController.ChangePassword) | ||
} |
Oops, something went wrong.