@@ -16,6 +16,15 @@ type (
16
16
// Skipper defines a function to skip middleware.
17
17
Skipper Skipper
18
18
19
+ // BeforeFunc defines a function which is executed just before the middleware.
20
+ BeforeFunc BeforeFunc
21
+
22
+ // SuccessHandler defines a function which is executed for a valid token.
23
+ SuccessHandler JWTSuccessHandler
24
+
25
+ // ErrorHandler defines a function which is executed for an invalid token.
26
+ ErrorHandler JWTErrorHandler
27
+
19
28
// Signing key to validate token.
20
29
// Required.
21
30
SigningKey interface {}
@@ -48,6 +57,12 @@ type (
48
57
keyFunc jwt.Keyfunc
49
58
}
50
59
60
+ // JWTSuccessHandler defines a function which is executed for a valid token.
61
+ JWTSuccessHandler func (echo.Context )
62
+
63
+ // JWTErrorHandler defines a function which is executed for an invalid token.
64
+ JWTErrorHandler func (echo.Context , echo.HandlerFunc ) error
65
+
51
66
jwtExtractor func (echo.Context ) (string , error )
52
67
)
53
68
@@ -137,6 +152,10 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
137
152
return next (c )
138
153
}
139
154
155
+ if config .BeforeFunc != nil {
156
+ config .BeforeFunc (c )
157
+ }
158
+
140
159
auth , err := extractor (c )
141
160
if err != nil {
142
161
return err
@@ -153,8 +172,14 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
153
172
if err == nil && token .Valid {
154
173
// Store user information from token into context.
155
174
c .Set (config .ContextKey , token )
175
+ if config .SuccessHandler != nil {
176
+ config .SuccessHandler (c )
177
+ }
156
178
return next (c )
157
179
}
180
+ if config .ErrorHandler != nil {
181
+ return config .ErrorHandler (c , next )
182
+ }
158
183
return & echo.HTTPError {
159
184
Code : ErrJWTInvalid .Code ,
160
185
Message : ErrJWTInvalid .Message ,
0 commit comments