Skip to content

Instantly share code, notes, and snippets.

@timbaev
Created January 26, 2019 13:49
Show Gist options
  • Save timbaev/23a5e69fae188a23c06ae0cbf923f82d to your computer and use it in GitHub Desktop.
Save timbaev/23a5e69fae188a23c06ae0cbf923f82d to your computer and use it in GitHub Desktop.
JWT Middleware
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