@@ -3,6 +3,7 @@ package jwt_test
33import (
44 "errors"
55 "fmt"
6+ "log"
67 "time"
78
89 "github.com/golang-jwt/jwt/v5"
@@ -24,7 +25,7 @@ func ExampleNewWithClaims_registeredClaims() {
2425
2526 token := jwt .NewWithClaims (jwt .SigningMethodHS256 , claims )
2627 ss , err := token .SignedString (mySigningKey )
27- fmt .Printf ( "%v %v" , ss , err )
28+ fmt .Println ( ss , err )
2829 // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.0XN_1Tpp9FszFOonIBpwha0c_SfnNI22DhTnjMshPg8 <nil>
2930}
3031
@@ -67,7 +68,7 @@ func ExampleNewWithClaims_customClaimsType() {
6768
6869 token := jwt .NewWithClaims (jwt .SigningMethodHS256 , claims )
6970 ss , err := token .SignedString (mySigningKey )
70- fmt .Printf ( "%v %v" , ss , err )
71+ fmt .Println ( ss , err )
7172
7273 // Output: foo: bar
7374 // eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.xVuY2FZ_MRXMIEgVQ7J-TFtaucVFRXUzHm9LmV41goM <nil>
@@ -86,11 +87,12 @@ func ExampleParseWithClaims_customClaimsType() {
8687 token , err := jwt .ParseWithClaims (tokenString , & MyCustomClaims {}, func (token * jwt.Token ) (interface {}, error ) {
8788 return []byte ("AllYourBase" ), nil
8889 })
89-
90- if claims , ok := token .Claims .(* MyCustomClaims ); ok && token .Valid {
91- fmt .Printf ("%v %v" , claims .Foo , claims .RegisteredClaims .Issuer )
90+ if err != nil {
91+ log .Fatal (err )
92+ } else if claims , ok := token .Claims .(* MyCustomClaims ); ok {
93+ fmt .Println (claims .Foo , claims .RegisteredClaims .Issuer )
9294 } else {
93- fmt . Println ( err )
95+ log . Fatal ( "unknown claims type, cannot proceed" )
9496 }
9597
9698 // Output: bar test
@@ -109,11 +111,12 @@ func ExampleParseWithClaims_validationOptions() {
109111 token , err := jwt .ParseWithClaims (tokenString , & MyCustomClaims {}, func (token * jwt.Token ) (interface {}, error ) {
110112 return []byte ("AllYourBase" ), nil
111113 }, jwt .WithLeeway (5 * time .Second ))
112-
113- if claims , ok := token .Claims .(* MyCustomClaims ); ok && token .Valid {
114- fmt .Printf ("%v %v" , claims .Foo , claims .RegisteredClaims .Issuer )
114+ if err != nil {
115+ log .Fatal (err )
116+ } else if claims , ok := token .Claims .(* MyCustomClaims ); ok {
117+ fmt .Println (claims .Foo , claims .RegisteredClaims .Issuer )
115118 } else {
116- fmt . Println ( err )
119+ log . Fatal ( "unknown claims type, cannot proceed" )
117120 }
118121
119122 // Output: bar test
@@ -147,11 +150,12 @@ func ExampleParseWithClaims_customValidation() {
147150 token , err := jwt .ParseWithClaims (tokenString , & MyCustomClaims {}, func (token * jwt.Token ) (interface {}, error ) {
148151 return []byte ("AllYourBase" ), nil
149152 }, jwt .WithLeeway (5 * time .Second ))
150-
151- if claims , ok := token .Claims .(* MyCustomClaims ); ok && token .Valid {
152- fmt .Printf ("%v %v" , claims .Foo , claims .RegisteredClaims .Issuer )
153+ if err != nil {
154+ log .Fatal (err )
155+ } else if claims , ok := token .Claims .(* MyCustomClaims ); ok {
156+ fmt .Println (claims .Foo , claims .RegisteredClaims .Issuer )
153157 } else {
154- fmt . Println ( err )
158+ log . Fatal ( "unknown claims type, cannot proceed" )
155159 }
156160
157161 // Output: bar test
0 commit comments