/
Routing.kt
51 lines (49 loc) · 1.64 KB
/
Routing.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.tomaszezula.ktorauth.ch02.plugins
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.html.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.sessions.*
import kotlinx.html.*
fun Application.configureRouting() {
routing {
authenticate("auth-session", "auth-form") {
get("/") {
call.respondText("Hello, ${call.principal<UserSession>()?.name}!")
}
post("/login") {
val username = call.principal<UserIdPrincipal>()?.name
username?.let { call.sessions.set(UserSession(it)) }
call.respondText("Hello, ${call.principal<UserIdPrincipal>()?.name}!")
}
}
get("/logout") {
call.sessions.clear<UserSession>()
call.respondRedirect("/login")
}
get("/login") {
call.respondHtml {
body {
form(
action = "/login",
encType = FormEncType.applicationXWwwFormUrlEncoded,
method = FormMethod.post
) {
p {
+"Username:"
textInput(name = "username")
}
p {
+"Password:"
passwordInput(name = "password")
}
p {
submitInput() { value = "Login" }
}
}
}
}
}
}
}