I am trying to understand Ktor form authentication...
# ktor
l
I am trying to understand Ktor form authentication. To do so, I have created a very simple web app but I am stuck on an error for multiple hours and documentation seems incomplete or outdated.
Copy code
fun Application.bank() {
    install(Authentication) {
        form("form") {
            challenge = FormAuthChallenge.Unauthorized
            passwordParamName = "password"
            userParamName = "username"
            validate { credentials ->
                println("Credentials : ${credentials.name}, ${credentials.password}")
                when {
                    credentials.name == "John" && credentials.password == "pa$$w0rd" -> UserIdPrincipal("John")
                    else -> null
                }
            }
        }
    }
    routing {
        authenticate("form") {
            route("account") {
                post {
                    call.respond(HttpStatusCode.OK, "connected")
                }
            }
        }
    }
}
To access the route /account I send this HTTP request :
POST <http://0.0.0.0:8081/account?username=Louis&password=pass>
But this raises an exception :
io.ktor.features.CannotTransformContentToTypeException: Cannot transform this request's content to class io.ktor.http.Parameters
So I guess I should not give credentials to the server via POST body. I have tried sending credentials in JSON with ContentNegocition installed but I get a similar error. If someone could help me ... I will update the documentation once I know how it works. Posted in #server
c
Looks like your POST is wrong. Why do you pass form parameters through URL when using POST method?
Ah, it looks like it is already resolved.
l
Yeah, thanks for your interest, but I initially posted this question in #server then I saw #ktor and I transfered the question in it.