Skip to content

How do I know if the token is expired or other errors? #133

Answered by oxisto
push-and-pop asked this question in Q&A
Discussion options

You must be logged in to vote

You need to access our specific error type ValidationError and then can access the bit mask to get the type of error. We are working to improve this. The first step is to support error unwrapping with #125 but this does not work consistently for all claims yet. So you need to do the following, if err is the error you get from Parse or ParseWithClaims.

var ve *jwt.ValidationError
if errors.As(err, &ve) {
	if ve.Errors&jwt.ValidationErrorExpired != 0 {
		// Token is either expired or not active yet
		fmt.Println("Timing is everything")
	}
}

Similar code can be found in the error checking example

jwt/example_test.go

Lines 96 to 119 in c435f38

func ExampleParse_errorChecking() {

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by oxisto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #124 on November 29, 2021 20:06.