-
Notifications
You must be signed in to change notification settings - Fork 69
feat: add refresh token endpoint and related configurations #9
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
Changes from 1 commit
20eee13
a3e5587
bee8aca
3fde9d1
5dadabb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,18 +7,27 @@ import ( | |||||||||||||||||||||||
| "github.com/naeemaei/golang-clean-web-api/api/dto" | ||||||||||||||||||||||||
| "github.com/naeemaei/golang-clean-web-api/api/helper" | ||||||||||||||||||||||||
| "github.com/naeemaei/golang-clean-web-api/config" | ||||||||||||||||||||||||
| "github.com/naeemaei/golang-clean-web-api/constant" | ||||||||||||||||||||||||
| "github.com/naeemaei/golang-clean-web-api/dependency" | ||||||||||||||||||||||||
| "github.com/naeemaei/golang-clean-web-api/usecase" | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| type UsersHandler struct { | ||||||||||||||||||||||||
| usecase *usecase.UserUsecase | ||||||||||||||||||||||||
| otpUsecase *usecase.OtpUsecase | ||||||||||||||||||||||||
| usecase *usecase.UserUsecase | ||||||||||||||||||||||||
| otpUsecase *usecase.OtpUsecase | ||||||||||||||||||||||||
| tokenUsecase *usecase.TokenUsecase | ||||||||||||||||||||||||
| config *config.Config | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| func NewUserHandler(cfg *config.Config) *UsersHandler { | ||||||||||||||||||||||||
| tokenUsecase := usecase.NewTokenUsecase(cfg) | ||||||||||||||||||||||||
| usecase := usecase.NewUserUsecase(cfg, dependency.GetUserRepository(cfg)) | ||||||||||||||||||||||||
| return &UsersHandler{usecase: usecase} | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return &UsersHandler{ | ||||||||||||||||||||||||
| usecase: usecase, | ||||||||||||||||||||||||
| tokenUsecase: tokenUsecase, | ||||||||||||||||||||||||
| config: cfg, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // LoginByUsername godoc | ||||||||||||||||||||||||
|
|
@@ -47,6 +56,9 @@ func (h *UsersHandler) LoginByUsername(c *gin.Context) { | |||||||||||||||||||||||
| return | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Set the refresh token in a cookie | ||||||||||||||||||||||||
| c.SetCookie(constant.RefreshTokenCookieName, token.RefreshToken, int(h.config.JWT.RefreshTokenExpireDuration*60), "/", h.config.Server.Domin, true, true) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| c.JSON(http.StatusCreated, helper.GenerateBaseResponse(token, true, helper.Success)) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -105,6 +117,9 @@ func (h *UsersHandler) RegisterLoginByMobileNumber(c *gin.Context) { | |||||||||||||||||||||||
| return | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Set the refresh token in a cookie | ||||||||||||||||||||||||
| c.SetCookie(constant.RefreshTokenCookieName, token.RefreshToken, int(h.config.JWT.RefreshTokenExpireDuration*60), "/", h.config.Server.Domin, true, true) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| c.SetCookie(constant.RefreshTokenCookieName, token.RefreshToken, int(h.config.JWT.RefreshTokenExpireDuration*60), "/", h.config.Server.Domin, true, true) | |
| http.SetCookie(c.Writer, &http.Cookie{ | |
| Name: constant.RefreshTokenCookieName, | |
| Value: token.RefreshToken, | |
| MaxAge: int(h.config.JWT.RefreshTokenExpireDuration * 60), | |
| Path: "/", | |
| Domain: h.config.Server.Domain, | |
| Secure: true, | |
| HttpOnly: true, | |
| SameSite: http.SameSiteStrictMode, | |
| }) |
Outdated
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.
Given that we are requesting a new token in refresh-token and not retrieving a persistent resource. so it's better to set method POST
| // @Router /v1/users/refresh-token [get] | |
| // @Router /v1/users/refresh-token [post] |
Outdated
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.
Same as previous changes
| c.SetCookie(constant.RefreshTokenCookieName, token.RefreshToken, int(h.config.JWT.RefreshTokenExpireDuration*60), "/", h.config.Server.Domin, true, true) | |
| http.SetCookie(c.Writer, &http.Cookie{ | |
| Name: constant.RefreshTokenCookieName, | |
| Value: token.RefreshToken, | |
| MaxAge: int(h.config.JWT.RefreshTokenExpireDuration * 60), | |
| Path: "/", | |
| Domain: h.config.Server.Domain, | |
| Secure: true, | |
| HttpOnly: true, | |
| SameSite: http.SameSiteStrictMode, | |
| }) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,4 +14,5 @@ func User(router *gin.RouterGroup, cfg *config.Config) { | |||||
| router.POST("/login-by-username", h.LoginByUsername) | ||||||
| router.POST("/register-by-username", h.RegisterByUsername) | ||||||
| router.POST("/login-by-mobile", h.RegisterLoginByMobileNumber) | ||||||
| router.GET("/refresh-token", h.RefreshToken) | ||||||
|
||||||
| router.GET("/refresh-token", h.RefreshToken) | |
| router.POST("/refresh-token", h.RefreshToken) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ server: | |||||
| internalPort: 5005 | ||||||
| externalPort: 5005 | ||||||
| runMode: debug | ||||||
| domin: localhost | ||||||
|
||||||
| domin: localhost | |
| domain: localhost |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ server: | |||||
| internalPort: 5000 | ||||||
| externalPort: 0 | ||||||
| runMode: release | ||||||
| domin: localhost | ||||||
|
||||||
| domin: localhost | |
| domain: localhost |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ server: | |||||
| internalPort: 5010 | ||||||
| externalPort: 5010 | ||||||
| runMode: release | ||||||
| domin: localhost | ||||||
|
||||||
| domin: localhost | |
| domain: localhost |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,9 +21,10 @@ type Config struct { | |||||
| } | ||||||
|
|
||||||
| type ServerConfig struct { | ||||||
| InternalPort string | ||||||
| ExternalPort string | ||||||
| RunMode string | ||||||
| InternalPort string | ||||||
| ExternalPort string | ||||||
| RunMode string | ||||||
| Domin string | ||||||
|
||||||
| Domin string | |
| Domain 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.
It's better to set sameSite attribute for CSRF protection