feat: Adds employee form and table view, begins user form and table view.

This commit is contained in:
2025-01-07 14:05:40 -05:00
parent e3f150b32c
commit 08a0a8e1a3
15 changed files with 366 additions and 93 deletions

View File

@@ -2,80 +2,6 @@ import Fluent
import Vapor
func routes(_ app: Application) throws {
let redirectMiddleware = User.redirectMiddleware(path: "login")
// let protected = app.grouped(redirectMiddleware)
let credentialsProtected = app.grouped(User.credentialsAuthenticator(), redirectMiddleware)
app.get { req async throws in
try await req.view.render(
"index",
Index(showNavLinks: false)
)
}
app.get("login") { req async throws -> View in
req.logger.info("login")
return try await req.view.render("login")
}
app.post("logout") { req async throws -> View in
req.auth.logout(User.self)
return try await req.view.render("login")
}
app.post("login") { req async throws -> View in
let content = try req.content.decode(UserForm.self)
guard let user = try await User.query(on: req.db)
.filter(\.$username == content.username)
.first()
else {
throw Abort(.badRequest, reason: "User not found.")
}
guard try user.verify(password: content.password) else {
throw Abort(.unauthorized, reason: "Invalid password.")
}
req.auth.login(user)
req.logger.debug("User: \(user.toDTO())")
return try await req.view.render("logged-in")
}
credentialsProtected.get("home") { req async throws in
req.logger.info("home")
return try await req.view.render("logged-in")
}
// TODO: Remove.
credentialsProtected.get("logged-in") { _ in
"Hello, logged-in!"
}
// app.get("index") { req async throws -> View in
//
// }
app.get("hello") { _ async -> String in
"Hello, world!"
}
try app.register(collection: ApiController())
}
struct Index: Content {
let title: String
let showNavLinks: Bool
init(
title: String = "HHE - Purchase Orders",
showNavLinks: Bool
) {
self.title = title
self.showNavLinks = showNavLinks
}
}
struct UserForm: Content {
let username: String
let password: String
try app.register(collection: ViewController())
}