-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
52 lines (43 loc) · 1.15 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
package auth
import (
"fmt"
"os"
"time"
"github.com/dgrijalva/jwt-go"
)
var secretKey = []byte(os.Getenv("SECRET_KEY"))
type CustomClaims struct {
Email string `json:"email"`
jwt.StandardClaims
}
func GenerateJWT(email string) (string, error) {
claims := CustomClaims{
Email: email,
StandardClaims: jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(time.Hour * 1).Unix(), // Token expires in 1 hour
},
}
// Create the token with the claims and sign it with the secret key
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
singedToken, err := token.SignedString(secretKey)
if err != nil {
return "", err
}
return singedToken, nil
}
func ValidateJWT(tokenString string) (*CustomClaims, error) {
// Parse the token with the secret key
token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(t *jwt.Token) (interface{}, error) {
return secretKey, nil
})
// Check for errors during parsing
if err != nil {
return nil, err
}
// Check if the token is valid
if claims, ok := token.Claims.(*CustomClaims); ok && token.Valid {
return claims, nil
}
return nil, fmt.Errorf("Invalid token")
}