// // File.swift // // // Created by Michiel Carman on 9/26/24. // import Vapor import Fluent struct UserSignup: Content { let username: String let password: String } struct NewSession: Content { let token: String let user: User.Public } extension UserSignup: Validatable { static func validations(_ validations: inout Validations) { validations.add("username", as: String.self, is: !.empty) validations.add("password", as: String.self, is: .count(6...)) } } struct UserController: RouteCollection { func boot(routes: any RoutesBuilder) throws { let users = routes.grouped("users") users.post(use: create) } @Sendable func create(req: Request) async throws -> Response{ let user = try req.content.decode(User.self) user.passwordHash = try Bcrypt.hash(user.passwordHash) try await user.save(on: req.db) let token = try Token.generate(for: user) try await token.save(on: req.db) return try await token.encodeResponse(for: req) } }