u
This commit is contained in:
1 parent
6849579652
commit
d60be4a75b
24 files changed
+463
-179
No files matched your search
@@ -15,9 +15,9 @@ type CustomClaims struct {
|
||||
|
||||
// TokenResult 用于返回验证结果
|
||||
type TokenResult struct {
|
||||
Claims *CustomClaims
|
||||
Valid bool
|
||||
Expired bool
|
||||
Claims *CustomClaims
|
||||
IsValid bool
|
||||
IsExpired bool
|
||||
}
|
||||
|
||||
// CreateAccessToken 创建 Access Token,只返回 token 和 error
|
||||
@@ -61,7 +61,7 @@ func createToken(userID int, expiry time.Duration) (string, error) {
|
||||
|
||||
// VerifyToken 验证 token 并返回结构化结果
|
||||
func VerifyToken(tokenString string) TokenResult {
|
||||
result := TokenResult{Valid: false}
|
||||
result := TokenResult{IsValid: false, IsExpired: false}
|
||||
|
||||
token, _ := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(config.App.JWT.Secret), nil
|
||||
@@ -73,9 +73,9 @@ func VerifyToken(tokenString string) TokenResult {
|
||||
}
|
||||
|
||||
result.Claims = claims
|
||||
result.Valid = true
|
||||
result.IsValid = true
|
||||
if claims.ExpiresAt != nil && time.Now().After(claims.ExpiresAt.Time) {
|
||||
result.Expired = true
|
||||
result.IsExpired = true
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
Reference in new issue
Block a user