File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,9 +21,9 @@ func GetAssignmentsForUserExperiment(repo *repository.Assignments) RequestProces
2121 return nil , err
2222 }
2323
24- userID := service .GetUserID (r .Context ())
25- if userID == 0 {
26- return nil , fmt . Errorf ( "no user id in context" )
24+ userID , err := service .GetUserID (r .Context ())
25+ if err != nil {
26+ return nil , err
2727 }
2828
2929 assignments , err := repo .GetAll (userID , experimentID )
@@ -50,9 +50,9 @@ func SaveAssignment(repo *repository.Assignments) RequestProcessFunc {
5050 return nil , err
5151 }
5252
53- userID := service .GetUserID (r .Context ())
54- if userID == 0 {
55- return nil , fmt . Errorf ( "no user id in context" )
53+ userID , err := service .GetUserID (r .Context ())
54+ if err != nil {
55+ return nil , err
5656 }
5757
5858 var assignmentRequest assignmentRequest
Original file line number Diff line number Diff line change 11package handler
22
33import (
4- "fmt"
54 "net/http"
65
76 "github.com/src-d/code-annotation/server/repository"
@@ -13,12 +12,12 @@ import (
1312// with the information about the current user
1413func Me (usersRepo * repository.Users ) RequestProcessFunc {
1514 return func (r * http.Request ) (* serializer.Response , error ) {
16- uID := service .GetUserID (r .Context ())
17- if uID == 0 {
18- return nil , fmt . Errorf ( "no user id in context" )
15+ userID , err := service .GetUserID (r .Context ())
16+ if err != nil {
17+ return nil , err
1918 }
2019
21- u , err := usersRepo .GetByID (uID )
20+ u , err := usersRepo .GetByID (userID )
2221 if err != nil || u == nil {
2322 return nil , serializer .NewHTTPError (http .StatusNotFound , "user not found" )
2423 }
Original file line number Diff line number Diff line change @@ -77,8 +77,19 @@ func (j *JWT) Middleware(next http.Handler) http.Handler {
7777 })
7878}
7979
80- // GetUserID gets user ID set by the JWT middleware, or 0 if was not set
81- func GetUserID (ctx context.Context ) int {
82- id , _ := ctx .Value (userIDKey ).(int )
83- return id
80+ // getUserInt gets the value stored in the Context for the key userIDKey, bool
81+ // is true on success
82+ func getUserInt (ctx context.Context ) (int , bool ) {
83+ i , ok := ctx .Value (userIDKey ).(int )
84+ return i , ok
85+ }
86+
87+ // GetUserID gets the user ID set by the JWT middleware in the Context
88+ func GetUserID (ctx context.Context ) (int , error ) {
89+ id , ok := getUserInt (ctx )
90+ if ! ok {
91+ return 0 , fmt .Errorf ("User ID is not set in the context" )
92+ }
93+
94+ return id , nil
8495}
You can’t perform that action at this time.
0 commit comments