Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Signed-off-by: William <[email protected]>
  • Loading branch information
kwesidev committed Dec 7, 2023
1 parent 4997aac commit 0d66718
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
18 changes: 5 additions & 13 deletions internal/controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func NewUserController(db *sql.DB) *UserController {

// Index Welcome user
func (usrCtrl *UserController) Index(w http.ResponseWriter, r *http.Request) {
claims := r.Context().Value("claims").(map[string]interface{})
userId := claims["userId"].(int)
userId := utilities.GetUserIdFromHttpConext(r)
userDetails := usrCtrl.userService.Get(userId)
utilities.JSONResponse(w, userDetails)
}
Expand All @@ -53,8 +52,7 @@ func (usrCtrl *UserController) Update(w http.ResponseWriter, r *http.Request) {
utilities.JSONError(w, err.Error(), http.StatusBadRequest)
return
}
claims := r.Context().Value("claims").(map[string]interface{})
userId := claims["userId"].(int)
userId := utilities.GetUserIdFromHttpConext(r)
response := models.SuccessResponse{}
err = usrCtrl.userService.Update(userId, userUpdateRequest)
if err != nil {
Expand All @@ -75,10 +73,8 @@ func (usrCtrl *UserController) Logout(w http.ResponseWriter, r *http.Request) {
return
}
response := models.SuccessResponse{}
claims := r.Context().Value("claims").(map[string]interface{})
userId := claims["userId"].(int)
userId := utilities.GetUserIdFromHttpConext(r)
success, err := usrCtrl.userService.DeleteToken(userId, tokenRefreshRequest.RefreshToken)

if err != nil {
response.Success = false
utilities.JSONError(w, "Failed to register", http.StatusBadRequest)
Expand All @@ -96,8 +92,7 @@ func (usrCtrl *UserController) EnableTwoFactor(w http.ResponseWriter, r *http.Re
utilities.JSONError(w, err.Error(), http.StatusBadRequest)
return
}
claims := r.Context().Value("claims").(map[string]interface{})
userId := claims["userId"].(int)
userId := utilities.GetUserIdFromHttpConext(r)
if enableTwoFactorRequest.Type == "TOTP" {
totpResponse, err := usrCtrl.userService.EnableTwoFactorTOTP(userId)
if err != nil {
Expand Down Expand Up @@ -133,15 +128,12 @@ func (usrCtrl *UserController) VerifyPassCode(w http.ResponseWriter, r *http.Req
utilities.JSONError(w, err.Error(), http.StatusBadRequest)
return
}
claims := r.Context().Value("claims").(map[string]interface{})
userId := claims["userId"].(int)
userId := utilities.GetUserIdFromHttpConext(r)
response := models.SuccessResponse{}

if usrCtrl.authService.VerifyPassCode(userId, verifyPassCodeRequest.Code) {
response.Success = true
} else {
response.Success = false
}
utilities.JSONResponse(w, response)

}
8 changes: 8 additions & 0 deletions internal/utilities/http_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"github.com/kwesidev/speedyauth/internal/models"
)

// GetUserId from bearer token stored in http header
func GetUserIdFromHttpConext(r *http.Request) int {
claims := r.Context().Value("claims").(map[string]interface{})
userId := claims["userId"].(int)
return userId
}

// Get JsonData from http request
func GetJsonInput(input interface{}, req *http.Request) error {
body, err := io.ReadAll(req.Body)
if err != nil {
Expand Down

0 comments on commit 0d66718

Please sign in to comment.