# Form Authentication in Ktor

**URL:** <https://discuss.kotlinlang.org/t/form-authentication-in-ktor/4815>\
**Category:** Support\
**Created:** [September 30, 2017, 6:23am UTC](https://discuss.kotlinlang.org/t/form-authentication-in-ktor/4815 "2017-09-30T06:23:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasan.oryx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hasan.oryx/32/5515_2.png) [@hasan.oryx](https://discuss.kotlinlang.org/u/hasan.oryx)\
**Post date:** [September 30, 2017, 6:23am UTC](https://discuss.kotlinlang.org/t/form-authentication-in-ktor/4815/1 "2017-09-30T06:23:36Z")

</div>

I’m new to `Kotlin` and `Ktor` trying to see the authentication part, so I got the below code.

Routes “/” and “/bye” are working fine, but route “login” given blank page!

```
package blog

import kotlinx.html.*
import kotlinx.html.stream.* // for createHTML
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.auth.*
import org.jetbrains.ktor.features.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.response.*
import org.jetbrains.ktor.routing.*

import org.jetbrains.ktor.request.* // for request.uri

import org.jetbrains.ktor.html.*
import org.jetbrains.ktor.pipeline.*

import org.jetbrains.ktor.host.* // for embededServer
import org.jetbrains.ktor.netty.* // for Netty

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start()
}

fun Application.module() {
    install(DefaultHeaders)
    install(CallLogging)

    intercept(ApplicationCallPipeline.Call) { 
        if (call.request.uri == "/hi")
            call.respondText("Test String")
    }

    install(Routing) {
        get("/") {
            call.respondText("""Hello, world!<br><a href="/bye">Say bye?</a>""", ContentType.Text.Html)
        }
        get("/bye") {
            call.respondText("""Good bye! <br><a href="/login">Login?</a> """, ContentType.Text.Html)
        }
        route("/login") {
            authentication {
                formAuthentication { up: UserPasswordCredential ->
                    when {
                        up.password == "ppp" -> UserIdPrincipal(up.name)
                        else -> null
                    }
                }
            }
         
            handle {
                val principal = call.authentication.principal<UserIdPrincipal>()
                if (principal != null) {
                    call.respondText("Hello, ${principal.name}")
                } else {
                        val html = createHTML().html {
                        body {
                            form(action = "/login", encType = FormEncType.applicationXWwwFormUrlEncoded, method = FormMethod.post) {
                                p {
                                    +"user:"
                                    textInput(name = "user") {
                                        value = principal?.name ?: ""
                                    }
                                }

                                p {
                                    +"password:"
                                    passwordInput(name = "pass")
                                }

                                p {
                                    submitInput() { value = "Login" }
                                }
                            }
                        }
                    }
                    call.respondText(html, ContentType.Text.Html)
                }
            }
        }
    }
}

```

When I disabled the authentication part below, the route ‘/login’ displayed the required form, which means the error is most likely in this part or in the way of calling it? I guess.

```
authentication {
    formAuthentication { up: UserPasswordCredential ->
        when {
            up.password == "ppp" -> UserIdPrincipal(up.name)
            else -> null
        }
    }
}

```
