feat: Adds middleware to routes

This commit is contained in:
2025-01-19 14:20:51 -05:00
parent b23dc6bf07
commit 185ebbbc15
6 changed files with 158 additions and 37 deletions

View File

@@ -1,10 +1,18 @@
import DatabaseClient
import DatabaseClientLive
import Dependencies
import Fluent
import SharedModels
import Vapor
private let apiMiddleware: [any Middleware] = [
UserPasswordAuthenticator(),
UserTokenAuthenticator(),
User.guardMiddleware()
]
extension ApiRoute {
var middleware: [any Middleware]? { apiMiddleware }
func handle(request: Request) async throws -> any AsyncResponseEncodable {
switch self {
case let .employee(route):
@@ -22,6 +30,7 @@ extension ApiRoute {
}
extension ApiRoute.EmployeeApiRoute {
func handle(request: Request) async throws -> any AsyncResponseEncodable {
@Dependency(\.database) var database
switch self {
@@ -44,6 +53,7 @@ extension ApiRoute.EmployeeApiRoute {
}
extension ApiRoute.PurchaseOrderApiRoute {
func handle(request: Request) async throws -> any AsyncResponseEncodable {
@Dependency(\.database.purchaseOrders) var purchaseOrders
switch self {
@@ -67,6 +77,7 @@ extension ApiRoute.PurchaseOrderApiRoute {
// TODO: Add Login.
extension ApiRoute.UserApiRoute {
func handle(request: Request) async throws -> any AsyncResponseEncodable {
@Dependency(\.database.users) var users
switch self {