Created
January 26, 2019 13:49
-
-
Save timbaev/23a5e69fae188a23c06ae0cbf923f82d to your computer and use it in GitHub Desktop.
JWT Middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vapor | |
import JWT | |
class JWTMiddleware: Middleware { | |
func respond(to request: Request, chainingTo next: Responder) throws -> EventLoopFuture<Response> { | |
if let token = request.http.headers[.authorization].first { | |
do { | |
try TokenHelpers.verifyToken(token) | |
return try next.respond(to: request) | |
} catch let error as JWTError { | |
throw Abort(.unauthorized, reason: error.reason) | |
} | |
} else { | |
throw Abort(.unauthorized, reason: "No Access Token") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment