Files
video-factory/common/auth.go
T
2026-07-24 14:59:45 +08:00

35 lines
696 B
Go

package common
import (
"errors"
"github.com/golang-jwt/jwt/v5"
)
const jwtSecret = "video-factory-jwt-secret-2024"
type JwtClaims struct {
UserId int64 `json:"user_id"`
Role string `json:"role"`
AgentId int64 `json:"agent_id,omitempty"`
jwt.RegisteredClaims
}
func GetJwtSecret() string {
return jwtSecret
}
func ParseToken(tokenStr string) (*JwtClaims, error) {
token, err := jwt.ParseWithClaims(tokenStr, &JwtClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(jwtSecret), nil
})
if err != nil {
return nil, err
}
claims, ok := token.Claims.(*JwtClaims)
if !ok || !token.Valid {
return nil, errors.New("invalid token")
}
return claims, nil
}