This commit is contained in:
oneao committed 2025-08-13 14:51:19 +08:00
1 parent ed8bee3d77
commit 7552670e4a
23 files changed
+380 -154

No files matched your search

@@ -1,4 +1,4 @@
package utils
package jwt
import (
commonConfig "base-framework/pkg/config"
@@ -22,20 +22,19 @@ type CustomClaims struct {
// CreateToken 创建一个 JWT token
func CreateToken(orgID, userID string) (string, error) {
expireTime := time.Now().Add(commonConfig.JWT.Expiry).Unix()
expireAt := time.Now().Add(commonConfig.JWT.Expiry)
claims := CustomClaims{
OrgID: orgID,
UserID: userID,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expireTime,
ExpiresAt: expireAt.Unix(),
IssuedAt: time.Now().Unix(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signKey := []byte(commonConfig.JWT.Secret)
return token.SignedString(signKey)
return token.SignedString([]byte(commonConfig.JWT.Secret))
}
// VerifyToken 验证并解析 JWT token
@@ -52,7 +51,8 @@ func VerifyToken(tokenString string) (*CustomClaims, error) {
if err != nil {
// 判断是否过期错误
if ve, ok := err.(*jwt.ValidationError); ok {
var ve *jwt.ValidationError
if errors.As(err, &ve) {
if ve.Errors&jwt.ValidationErrorExpired != 0 {
return nil, ErrTokenExpired
}