-
Notifications
You must be signed in to change notification settings - Fork 50
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
base: master
Are you sure you want to change the base?
Changes from all commits
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,6 +7,7 @@ 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" | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
@@ -48,6 +49,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)) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -106,6 +110,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) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
c.JSON(http.StatusCreated, helper.GenerateBaseResponse(token, true, helper.Success)) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -137,3 +144,25 @@ func (h *UsersHandler) SendOtp(c *gin.Context) { | |||||||||||||||||||||||
// TODO: Call internal SMS service | ||||||||||||||||||||||||
c.JSON(http.StatusCreated, helper.GenerateBaseResponse(nil, true, helper.Success)) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// RefreshToken godoc | ||||||||||||||||||||||||
// @Summary RefreshToken | ||||||||||||||||||||||||
// @Description RefreshToken | ||||||||||||||||||||||||
// @Tags Users | ||||||||||||||||||||||||
// @Accept json | ||||||||||||||||||||||||
// @Produce json | ||||||||||||||||||||||||
// @Success 200 {object} helper.BaseHttpResponse "Success" | ||||||||||||||||||||||||
// @Failure 400 {object} helper.BaseHttpResponse "Failed" | ||||||||||||||||||||||||
// @Failure 401 {object} helper.BaseHttpResponse "Failed" | ||||||||||||||||||||||||
// @Router /v1/users/refresh-token [get] | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||
func (h *UsersHandler) RefreshToken(c *gin.Context) { | ||||||||||||||||||||||||
token, err := h.tokenUsecase.RefreshToken(c) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
c.AbortWithStatusJSON(helper.TranslateErrorToStatusCode(err), | ||||||||||||||||||||||||
helper.GenerateBaseResponseWithError(nil, false, helper.InternalError, err)) | ||||||||||||||||||||||||
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) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as previous changes
Suggested change
|
||||||||||||||||||||||||
c.JSON(http.StatusOK, helper.GenerateBaseResponse(token, true, helper.Success)) | ||||||||||||||||||||||||
} |
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based previous comment
Suggested change
|
||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ server: | |||||
internalPort: 5005 | ||||||
externalPort: 5005 | ||||||
runMode: debug | ||||||
domin: localhost | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
logger: | ||||||
filePath: ../logs/ | ||||||
encoding: json | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ server: | |||||
internalPort: 5000 | ||||||
externalPort: 0 | ||||||
runMode: release | ||||||
domin: localhost | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
logger: | ||||||
filePath: /app/logs/ | ||||||
encoding: json | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ server: | |||||
internalPort: 5010 | ||||||
externalPort: 5010 | ||||||
runMode: release | ||||||
domin: localhost | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
logger: | ||||||
filePath: logs/ | ||||||
encoding: json | ||||||
|
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
type LoggerConfig struct { | ||||||
|
@@ -93,10 +94,10 @@ func GetConfig() *Config { | |||||
|
||||||
cfg, err := ParseConfig(v) | ||||||
envPort := os.Getenv("PORT") | ||||||
if envPort != ""{ | ||||||
if envPort != "" { | ||||||
cfg.Server.ExternalPort = envPort | ||||||
log.Printf("Set external port from environment -> %s", cfg.Server.ExternalPort) | ||||||
}else{ | ||||||
} else { | ||||||
cfg.Server.ExternalPort = cfg.Server.InternalPort | ||||||
log.Printf("Set external port from environment -> %s", cfg.Server.ExternalPort) | ||||||
} | ||||||
|
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