Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alielmi98
Copy link

@alielmi98 alielmi98 commented Mar 21, 2025

New Feature:
-Implemented the RefreshToken method in the TokenUsecase to handle token refreshing. -The method retrieves the refresh token from the HTTP cookie, validates it, and generates a new access and refresh token pair.

Details:
-Extracts the refresh token from the cookie using c.Cookie. -Validates the refresh token and extracts claims using the GetClaims method. -Converts roles from []interface{} to []string for proper type handling. -Generates a new token pair using the GenerateToken method.

Reason for Addition:
-To provide functionality for refreshing expired access tokens while maintaining security through refresh tokens. -This is a critical feature for session management in the application.

Benefits:
-Enables secure token lifecycle management.
-Improves user experience by allowing seamless token refresh without requiring re-login.

New Feature:
-Implemented the RefreshToken method in the TokenUsecase to handle token refreshing.
-The method retrieves the refresh token from the HTTP cookie, validates it, and generates a new access and refresh token pair.

Details:
-Extracts the refresh token from the cookie using c.Cookie.
-Validates the refresh token and extracts claims using the GetClaims method.
-Converts roles from []interface{} to []string for proper type handling.
-Generates a new token pair using the GenerateToken method.

Reason for Addition:
-To provide functionality for refreshing expired access tokens while maintaining security through refresh tokens.
-This is a critical feature for session management in the application.

Benefits:
-Enables secure token lifecycle management.
-Improves user experience by allowing seamless token refresh without requiring re-login.
@naeemaei naeemaei assigned naeemaei and unassigned naeemaei Mar 31, 2025
@naeemaei
Copy link
Owner

Thanks, @alielmi98 for these valuable pull requests.
Please read my review messages

@alielmi98
Copy link
Author

But i can't find your review messages?

@@ -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)
Copy link
Owner

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

Suggested change
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,
})

@@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,
})

// @Success 200 {object} helper.BaseHttpResponse "Success"
// @Failure 400 {object} helper.BaseHttpResponse "Failed"
// @Failure 401 {object} helper.BaseHttpResponse "Failed"
// @Router /v1/users/refresh-token [get]
Copy link
Owner

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

Suggested change
// @Router /v1/users/refresh-token [get]
// @Router /v1/users/refresh-token [post]

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as previous changes

Suggested change
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,
})

@@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based previous comment

Suggested change
router.GET("/refresh-token", h.RefreshToken)
router.POST("/refresh-token", h.RefreshToken)

@@ -2,6 +2,7 @@ server:
internalPort: 5005
externalPort: 5005
runMode: debug
domin: localhost
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
domin: localhost
domain: localhost

@@ -2,6 +2,7 @@ server:
internalPort: 5000
externalPort: 0
runMode: release
domin: localhost
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
domin: localhost
domain: localhost

@@ -2,6 +2,7 @@ server:
internalPort: 5010
externalPort: 5010
runMode: release
domin: localhost
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
domin: localhost
domain: localhost

InternalPort string
ExternalPort string
RunMode string
Domin string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Domin string
Domain string

@naeemaei
Copy link
Owner

naeemaei commented May 5, 2025

Please check. @alielmi98

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants