Isn’t this going to result in the Token table just growing without bound? I’m thinking we should implement an “upsert” type action here so that a user only ever has a single token. Would there ever be a case where it would make sense for them to have more than just the most recent one listed? Maybe something like this?
func loginHandler(_ req: Request) throws -> Future<Token> {
let user = try req.requireAuthenticated(User.self)
let token = try Token.generate(for: user)
let id = try user.requireID()
return Token.query(on: req)
.filter(\.userID == id)
.delete()
.flatMap(to: Token.self) { _ in
return token.save(on: req)
}
}