diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-14 12:27:40 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-14 12:27:40 +0530 |
commit | eb1cdae78162d0bf6ef8caf56197d3696085f54b (patch) | |
tree | 4807fb01ee66353240830cab281d25e11bdfe43f | |
parent | 2f239481cdd750c2cbe85b012bdeb69841298c42 (diff) |
generating a new refresh token when it's close to expiryv0.24.0
-rw-r--r-- | auth/controller.go | 27 | ||||
-rw-r--r-- | main.go | 2 |
2 files changed, 27 insertions, 2 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, }) } @@ -38,7 +38,7 @@ import ( "log" ) -const OPENBILLS_VERSION = "v0.23.1" +const OPENBILLS_VERSION = "v0.24.0" func init() { if !viper.GetBool("debug_mode") { |