Skip to content

Commit

Permalink
fix access token check validate bug
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza-fa committed Feb 19, 2024
1 parent 3abb5fa commit 8130a8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
9 changes: 0 additions & 9 deletions src/api/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/alireza-fa/blog-go/src/api/helper"
"github.com/alireza-fa/blog-go/src/constants"
"github.com/alireza-fa/blog-go/src/services"
"github.com/golang-jwt/jwt/v5"
"net/http"
"strings"
)
Expand All @@ -23,14 +22,6 @@ func Authentication(next http.Handler) http.Handler {
err = errors.New("token is required")
} else {
claimMap, err = tokenService.GetClaims(token[1])
if err != nil {
switch {
case errors.Is(err, jwt.ErrTokenExpired):
err = errors.New("token expired")
default:
err = errors.New("token is invalid")
}
}
}
if err != nil {
helper.BaseResponseWithError(w, nil, http.StatusUnauthorized, err)
Expand Down
9 changes: 9 additions & 0 deletions src/services/token_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ func (s *TokenService) VerifyAccessToken(token string) (*jwt.Token, error) {
if err != nil {
return nil, err
}
claims, ok := at.Claims.(jwt.MapClaims)
if !ok {
return nil, err
}
expire := claims[constants.ExpireTimeKey].(float64)
if expire < float64(time.Now().Unix()) {
return nil, errors.New("token is expired")
}

return at, nil
}

Expand Down

0 comments on commit 8130a8a

Please sign in to comment.