46 lines
1.1 KiB
Swift
46 lines
1.1 KiB
Swift
import DatabaseClientLive
|
|
import SharedModels
|
|
import Vapor
|
|
|
|
extension RoutesBuilder {
|
|
|
|
// Used to ensure views are protected, redirects users to the login page if they're
|
|
// not authenticated.
|
|
var protected: any RoutesBuilder {
|
|
// #if DEBUG
|
|
// return self
|
|
// #else
|
|
return grouped(
|
|
// User.credentialsAuthenticator(),
|
|
UserPasswordAuthenticator(),
|
|
UserTokenAuthenticator(),
|
|
UserSessionAuthenticator(),
|
|
User.redirectMiddleware { req in
|
|
"login?next=\(req.url)"
|
|
}
|
|
)
|
|
// #endif
|
|
}
|
|
|
|
func apiUnprotected(route: PathComponent) -> any RoutesBuilder {
|
|
grouped("api", "v1", route)
|
|
}
|
|
|
|
// Allows basic or token authentication for api routes and prefixes the
|
|
// given route with "/api/v1".
|
|
func apiProtected(route: PathComponent) -> any RoutesBuilder {
|
|
// #if DEBUG
|
|
// return apiUnprotected(route: route)
|
|
// #else
|
|
let prefixed = grouped("api", "v1", route)
|
|
return prefixed.grouped(
|
|
UserPasswordAuthenticator(),
|
|
UserTokenAuthenticator(),
|
|
// User.authenticator(),
|
|
// UserToken.authenticator(),
|
|
User.guardMiddleware()
|
|
)
|
|
// #endif
|
|
}
|
|
}
|