Extend access token lifetime

This commit is contained in:
Ivan R. 2023-09-03 16:09:51 +05:00
parent 7a803688de
commit a1b648da91
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C

View file

@ -91,7 +91,7 @@ func AuthMiddleware(c *gin.Context, db *gorm.DB, cfg *config.Config) {
} }
// Create a new token if the old one is about to expire // Create a new token if the old one is about to expire
if time.Now().Add(12 * time.Hour).After(claims.ExpiresAt.Time) { if time.Now().Add(time.Hour * 24 * 3).After(claims.ExpiresAt.Time) {
newToken, err := GetJWTToken(cfg) newToken, err := GetJWTToken(cfg)
if err != nil { if err != nil {
ShowError(c, err) ShowError(c, err)
@ -103,7 +103,7 @@ func AuthMiddleware(c *gin.Context, db *gorm.DB, cfg *config.Config) {
func GetJWTToken(cfg *config.Config) (string, error) { func GetJWTToken(cfg *config.Config) (string, error) {
claims := jwt.RegisteredClaims{ claims := jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)), ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24 * 7)),
} }
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(cfg.SecretKey)) return token.SignedString([]byte(cfg.SecretKey))