-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt.go
60 lines (51 loc) · 1.43 KB
/
jwt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package jwt
import (
"log"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/ibilalkayy/proctl/database/redis"
"github.com/ibilalkayy/proctl/middleware"
)
// GenerateJWT generates a new JSON Web Token (JWT) with a 5-minute expiration time
func GenerateJWT() (string, bool) {
expirationTime := time.Now().Add(5 * time.Minute)
claims := jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
}
// Load the access secret from environment variables
byteEnv := []byte(middleware.LoadEnvVariable("ACCESS_SECRET"))
// Create a new token with the claims and signing method
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Generate the signed string token
tokenString, err := token.SignedString(byteEnv)
if err != nil {
log.Fatal(err)
}
return tokenString, true
}
// RefreshToken refreshes the JWT by generating a new token and updating it in Redis
func RefreshToken(tokenType string) bool {
var tokenKey string
switch tokenType {
case "user":
tokenKey = "LoginToken"
case "member":
tokenKey = "MemberLoginToken"
default:
return false
}
// Check if the token expiration is within 30 seconds
if time.Until(time.Now().Add(time.Duration(jwt.StandardClaims{}.ExpiresAt))) > 30*time.Second {
return false
} else {
// Generate a new JWT token
tokenString, ok := GenerateJWT()
if ok {
// Update the token in Redis
redis.SetAccountInfo(tokenKey, tokenString)
return true
} else {
return false
}
}
}