aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/auth/controller.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2025-10-14 12:27:40 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2025-10-14 12:27:40 +0530
commiteb1cdae78162d0bf6ef8caf56197d3696085f54b (patch)
tree4807fb01ee66353240830cab281d25e11bdfe43f /auth/controller.go
parent2f239481cdd750c2cbe85b012bdeb69841298c42 (diff)
generating a new refresh token when it's close to expiryv0.24.0
Diffstat (limited to 'auth/controller.go')
-rw-r--r--auth/controller.go27
1 files changed, 26 insertions, 1 deletions
diff --git a/auth/controller.go b/auth/controller.go
index c5c931a..86c0b2c 100644
--- a/auth/controller.go
+++ b/auth/controller.go
@@ -109,7 +109,7 @@ func handleSignIn(ctx *gin.Context) {
RefreshClaims{
jwt.RegisteredClaims{
IssuedAt: jwt.NewNumericDate(time.Now()),
- ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 6)),
+ ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 60)),
},
u.ID,
u.TokenVersion,
@@ -197,7 +197,32 @@ func handleRefresh(ctx *gin.Context) {
return
}
+ var refreshToken string
+ eat := claims.ExpiresAt.Unix()
+ if eat != 0 && eat < time.Now().Add(time.Hour * 6).Unix() {
+ // if refresh token expires in less than 6 hours, get a new one
+ refreshToken, err = jwt.NewWithClaims(jwt.SigningMethodHS256,
+ RefreshClaims{
+ jwt.RegisteredClaims{
+ IssuedAt: jwt.NewNumericDate(time.Now()),
+ ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 60)),
+ },
+ u.ID,
+ u.TokenVersion,
+ },
+ ).SignedString(REFRESH_KEY)
+ if err != nil {
+ // TODO: handle potential errors
+ ctx.Error(err)
+ ctx.Abort()
+ return
+ }
+ } else {
+ refreshToken = req.RefreshToken
+ }
+
ctx.JSON(http.StatusOK, gin.H{
"auth_token": authToken,
+ "refresh_token": refreshToken,
})
}