diff --git a/Sources/ImperialCore/Middleware/ImperialMiddleware.swift b/Sources/ImperialCore/Middleware/ImperialMiddleware.swift index acb0ea1..e32b4f6 100644 --- a/Sources/ImperialCore/Middleware/ImperialMiddleware.swift +++ b/Sources/ImperialCore/Middleware/ImperialMiddleware.swift @@ -5,26 +5,34 @@ public class ImperialMiddleware: Middleware { /// The path to redirect the user to if they are not authenticated. let redirectPath: String? + /// The Middleware for responding after error + let onErrorMiddleware: Middleware? /// Creates an instance of `ImperialMiddleware` with the option of a redirect path. /// /// - Parameter redirect: The path to redirect a user to if they do not have an access token. - public init(redirect: String? = nil) { + /// - Parameter onErrorMiddleware: The middleware for responding on oAuth error (might be used for e.g. for manual Authenticable) + public init(redirect: String? = nil, onErrorMiddleware: Middleware? = nil) { self.redirectPath = redirect + self.onErrorMiddleware = onErrorMiddleware } - /// Checks that the request contains an access token. If it does, let the request through. If not, redirect the user to the `redirectPath`. + /// Checks that the request contains an access token. If it does, let the request through. + /// If not, calls respond method from `onErrorMiddleware` if it exists. if not, redirect the user to the `redirectPath`. /// If the `redirectPath` is `nil`, then throw the error from getting the access token (Abort.unauthorized). public func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture { do { _ = try request.accessToken() return next.respond(to: request) } catch let error as Abort where error.status == .unauthorized { - guard let redirectPath = redirectPath else { - return request.eventLoop.makeFailedFuture(error) + guard let onErrorMiddleware = onErrorMiddleware else { + guard let redirectPath = redirectPath else { + return request.eventLoop.makeFailedFuture(error) + } + let redirect: Response = request.redirect(to: redirectPath) + return request.eventLoop.makeSucceededFuture(redirect) } - let redirect: Response = request.redirect(to: redirectPath) - return request.eventLoop.makeSucceededFuture(redirect) + return onErrorMiddleware.respond(to: request, chainingTo: next) } catch let error { return request.eventLoop.makeFailedFuture(error) }