Skip to content

Commit 0d66718

Browse files
committed
Refactor
Signed-off-by: William <[email protected]>
1 parent 4997aac commit 0d66718

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

internal/controllers/user_controller.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ func NewUserController(db *sql.DB) *UserController {
3333

3434
// Index Welcome user
3535
func (usrCtrl *UserController) Index(w http.ResponseWriter, r *http.Request) {
36-
claims := r.Context().Value("claims").(map[string]interface{})
37-
userId := claims["userId"].(int)
36+
userId := utilities.GetUserIdFromHttpConext(r)
3837
userDetails := usrCtrl.userService.Get(userId)
3938
utilities.JSONResponse(w, userDetails)
4039
}
@@ -53,8 +52,7 @@ func (usrCtrl *UserController) Update(w http.ResponseWriter, r *http.Request) {
5352
utilities.JSONError(w, err.Error(), http.StatusBadRequest)
5453
return
5554
}
56-
claims := r.Context().Value("claims").(map[string]interface{})
57-
userId := claims["userId"].(int)
55+
userId := utilities.GetUserIdFromHttpConext(r)
5856
response := models.SuccessResponse{}
5957
err = usrCtrl.userService.Update(userId, userUpdateRequest)
6058
if err != nil {
@@ -75,10 +73,8 @@ func (usrCtrl *UserController) Logout(w http.ResponseWriter, r *http.Request) {
7573
return
7674
}
7775
response := models.SuccessResponse{}
78-
claims := r.Context().Value("claims").(map[string]interface{})
79-
userId := claims["userId"].(int)
76+
userId := utilities.GetUserIdFromHttpConext(r)
8077
success, err := usrCtrl.userService.DeleteToken(userId, tokenRefreshRequest.RefreshToken)
81-
8278
if err != nil {
8379
response.Success = false
8480
utilities.JSONError(w, "Failed to register", http.StatusBadRequest)
@@ -96,8 +92,7 @@ func (usrCtrl *UserController) EnableTwoFactor(w http.ResponseWriter, r *http.Re
9692
utilities.JSONError(w, err.Error(), http.StatusBadRequest)
9793
return
9894
}
99-
claims := r.Context().Value("claims").(map[string]interface{})
100-
userId := claims["userId"].(int)
95+
userId := utilities.GetUserIdFromHttpConext(r)
10196
if enableTwoFactorRequest.Type == "TOTP" {
10297
totpResponse, err := usrCtrl.userService.EnableTwoFactorTOTP(userId)
10398
if err != nil {
@@ -133,15 +128,12 @@ func (usrCtrl *UserController) VerifyPassCode(w http.ResponseWriter, r *http.Req
133128
utilities.JSONError(w, err.Error(), http.StatusBadRequest)
134129
return
135130
}
136-
claims := r.Context().Value("claims").(map[string]interface{})
137-
userId := claims["userId"].(int)
131+
userId := utilities.GetUserIdFromHttpConext(r)
138132
response := models.SuccessResponse{}
139-
140133
if usrCtrl.authService.VerifyPassCode(userId, verifyPassCodeRequest.Code) {
141134
response.Success = true
142135
} else {
143136
response.Success = false
144137
}
145138
utilities.JSONResponse(w, response)
146-
147139
}

internal/utilities/http_util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import (
99
"github.com/kwesidev/speedyauth/internal/models"
1010
)
1111

12+
// GetUserId from bearer token stored in http header
13+
func GetUserIdFromHttpConext(r *http.Request) int {
14+
claims := r.Context().Value("claims").(map[string]interface{})
15+
userId := claims["userId"].(int)
16+
return userId
17+
}
18+
19+
// Get JsonData from http request
1220
func GetJsonInput(input interface{}, req *http.Request) error {
1321
body, err := io.ReadAll(req.Body)
1422
if err != nil {

0 commit comments

Comments
 (0)