I'm working on my very first Vapor app and I am trying to figure out how to make a route return either a View or a Response. The best solution I have been able to come up with so far is to make the routes return a Response and then call .encodeResponse(status: HTTPStatus, headers: HTTPHeaders, for: Request) on req.view.render to turn it into a Response.
The reason I want to do this is to be able to display form errors in case an invalid username or password has been entered (or invalid form input in general) and also to be able to redirect the user in case they provide valid form input.
The following code snippet works but I am not sure if this is an acceptable way of doing it? or if there is a better way of doing it?
Interesting. approach to the problem. Personally, I have not yet reached that stage in my playing. But I am wondering if the use of middleware would be a better strategy. This way whether you are using username/password, MFA, TOTP (Time-based One Time Password), JWT (Java Web Token) or something more insane like introducing IODC (Open ID Connect) or SAML. your app would not need to care. The middleware would have already looked at the user and decided to trust or not and add the appropriate items to the session to let your page code handle it.
I have not reached that section of Server Side Swift yet to see how he likes to do it. But it's the only book that I have found that was updated when Vapor went Async Await aware. In the book we have already addressed using middleware to fix minor routing errors.