/
jwt-auth.go
62 lines (51 loc) · 1.48 KB
/
jwt-auth.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
61
62
package pancham
import (
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
"go.mongodb.org/mongo-driver/bson/primitive"
)
var secretKey = []byte("pancham")
type CustomClaims struct {
UserID primitive.ObjectID `json:"userId"`
Username string `json:"username"`
Email string `json:"email"`
Name string `json:"name"`
Mobile string `json:"mobile"`
jwt.StandardClaims
}
func CreateToken(userID primitive.ObjectID, username string, name string, email string, mobile string) (string, error) {
// Set up the claims
claims := CustomClaims{
UserID: userID,
Username: username,
Name: name,
Email: email,
Mobile: mobile,
StandardClaims: jwt.StandardClaims{
// Token expiration time (e.g., 24 hours)
ExpiresAt: time.Now().Add(time.Hour * 240000).Unix(),
},
}
// Create the token with the claims and the secret key
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signedToken, err := token.SignedString(secretKey)
if err != nil {
return "", err
}
return signedToken, nil
}
func ValidateToken(tokenString string) (*CustomClaims, error) {
// Parse the token
token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return secretKey, nil
})
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")
}