@@ -33,8 +33,7 @@ func NewUserController(db *sql.DB) *UserController {
33
33
34
34
// Index Welcome user
35
35
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 )
38
37
userDetails := usrCtrl .userService .Get (userId )
39
38
utilities .JSONResponse (w , userDetails )
40
39
}
@@ -53,8 +52,7 @@ func (usrCtrl *UserController) Update(w http.ResponseWriter, r *http.Request) {
53
52
utilities .JSONError (w , err .Error (), http .StatusBadRequest )
54
53
return
55
54
}
56
- claims := r .Context ().Value ("claims" ).(map [string ]interface {})
57
- userId := claims ["userId" ].(int )
55
+ userId := utilities .GetUserIdFromHttpConext (r )
58
56
response := models.SuccessResponse {}
59
57
err = usrCtrl .userService .Update (userId , userUpdateRequest )
60
58
if err != nil {
@@ -75,10 +73,8 @@ func (usrCtrl *UserController) Logout(w http.ResponseWriter, r *http.Request) {
75
73
return
76
74
}
77
75
response := models.SuccessResponse {}
78
- claims := r .Context ().Value ("claims" ).(map [string ]interface {})
79
- userId := claims ["userId" ].(int )
76
+ userId := utilities .GetUserIdFromHttpConext (r )
80
77
success , err := usrCtrl .userService .DeleteToken (userId , tokenRefreshRequest .RefreshToken )
81
-
82
78
if err != nil {
83
79
response .Success = false
84
80
utilities .JSONError (w , "Failed to register" , http .StatusBadRequest )
@@ -96,8 +92,7 @@ func (usrCtrl *UserController) EnableTwoFactor(w http.ResponseWriter, r *http.Re
96
92
utilities .JSONError (w , err .Error (), http .StatusBadRequest )
97
93
return
98
94
}
99
- claims := r .Context ().Value ("claims" ).(map [string ]interface {})
100
- userId := claims ["userId" ].(int )
95
+ userId := utilities .GetUserIdFromHttpConext (r )
101
96
if enableTwoFactorRequest .Type == "TOTP" {
102
97
totpResponse , err := usrCtrl .userService .EnableTwoFactorTOTP (userId )
103
98
if err != nil {
@@ -133,15 +128,12 @@ func (usrCtrl *UserController) VerifyPassCode(w http.ResponseWriter, r *http.Req
133
128
utilities .JSONError (w , err .Error (), http .StatusBadRequest )
134
129
return
135
130
}
136
- claims := r .Context ().Value ("claims" ).(map [string ]interface {})
137
- userId := claims ["userId" ].(int )
131
+ userId := utilities .GetUserIdFromHttpConext (r )
138
132
response := models.SuccessResponse {}
139
-
140
133
if usrCtrl .authService .VerifyPassCode (userId , verifyPassCodeRequest .Code ) {
141
134
response .Success = true
142
135
} else {
143
136
response .Success = false
144
137
}
145
138
utilities .JSONResponse (w , response )
146
-
147
139
}
0 commit comments