-
Notifications
You must be signed in to change notification settings - Fork 4
User Authentication and Authorization using JWT #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Conversation
…d necessary comments
db/user.go
Outdated
| Country string `db:"country" json:"country"` | ||
| State string `db:"state" json:"state"` | ||
| City string `db:"city" json:"city"` | ||
| CreatedAt string `db:"created_at" json:"created_at"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use time.Time instead of string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually we are thinking of removing it since we are neither accepting it from the frontend nor we are sending it back.
service/user_http.go
Outdated
| return | ||
| } | ||
|
|
||
| user, err1 := deps.Store.GetUser(req.Context(), int(userID)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we rename it to err instead of err1?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes Sure
service/user_http.go
Outdated
| func getUserHandler(deps Dependencies) http.HandlerFunc { | ||
| return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
| //fetch usedId from request | ||
| authToken := req.Header["Token"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is a Get method on this Header map, can we use that instead of accessing it directly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes On it
| } | ||
|
|
||
| //generateJWT function generates and return a new JWT token | ||
| func generateJwt(userID int) (tokenString string, err error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, Naked returns are okay for smaller functions.. however we should avoid them for longer functions..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes Indeed, Will change this in the next commit.
service/session_http.go
Outdated
| } | ||
|
|
||
| respBytes, err := json.Marshal(authbody) | ||
| if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally json.Marshal doesn't fail.. however we shouldn't ignore this error..
can we return 5xx from here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure will return from here.
service/session_http.go
Outdated
| err = deps.Store.CreateBlacklistedToken(req.Context(), userBlackListedToken) | ||
| if err != nil { | ||
| ae.Error(ae.ErrFailedToCreate, "Error creating blaclisted token record", err) | ||
| rw.Header().Add("Content-Type", "application/json") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use Header.Set method instead of Header.Add?
because multiple Content-Type doesn't make sense here..
Set method replaces the value of existing.. and Add method appends the value..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, will use Header.Set method
service/session_http.go
Outdated
| authToken := req.Header["Token"] | ||
|
|
||
| //fetching details from the token | ||
| userID, expirationTimeStamp, err := getDataFromToken(authToken[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, authToken[0] will be index out of range if we don't pass the Token in header.. can we fix this?
or we can use Header.Get method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will use Header.Get method
Tasks Completed :