Changed jwt expiration to use hours instead of seconds
This commit is contained in:
parent
860135aff1
commit
1a5eb031e5
@ -11,4 +11,4 @@ DB_DATABASE=""
|
|||||||
|
|
||||||
# JWT Authentication
|
# JWT Authentication
|
||||||
JWT_SECRET="abc123"
|
JWT_SECRET="abc123"
|
||||||
JWT_EXPIRATION="3600"
|
JWT_EXPIRATION="8766"
|
||||||
|
@ -3,6 +3,7 @@ package helper
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
@ -22,14 +23,14 @@ func GenerateToken(userId string) (string, error) {
|
|||||||
return "", errors.New("JWT_EXPIRATION is not set")
|
return "", errors.New("JWT_EXPIRATION is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
expiration, err := time.ParseDuration(jwtExpiration + "s")
|
jwtExpirationInt, err := strconv.Atoi(jwtExpiration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("invalid JWT_EXPIRATION value")
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
claims := jwt.MapClaims{
|
claims := jwt.MapClaims{
|
||||||
"userId": userId,
|
"userId": userId,
|
||||||
"exp": time.Now().Add(expiration).Unix(),
|
"exp": time.Now().Add(time.Duration(jwtExpirationInt) * time.Hour).Unix(),
|
||||||
}
|
}
|
||||||
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user