keochris
04/08/2020, 6:31 PMfun Application.mainModule() {
// Intall the sessions feature with [MySession] class stored in the "MYSESSION_ID" cookie authenticated
// to prevent manipulation, unless the [secretHashKey] has been leaked.
install(Sessions) {
cookie<MySession>("MYSESSION_ID") {
transform(SessionTransportTransformerMessageAuthentication(secretHashKey))
}
}
// The StatusPages features allow to catch unhandled exceptions.
// We are going to use it to catch redirection exceptions and actually perform redirects
// We are also going to use it to catch exceptions of sessions not found to redirect to our desired pages.
install(StatusPages) {
registerRedirections()
registerSessionNotFoundRedirect<MySession>("/login")
}
// The routing feature allows to execute different code based on the request paths and http methods.
routing {
// For the '/' GET route, we are going to try to get a session (if not found it will redirect to the /login page)
// And if successfully, we will show the user name, and will show a button for logging out.
get("/") {
val session = call.sessions.getOrThrow<MySession>()
call.respondHtml {
body {
p { +"Logged as ${session.user}" }
p { +"Your tokenValue is ${token_Value}" }
form(action = "/logout", method = <http://FormMethod.post|FormMethod.post>) {
input(InputType.submit) { value = "Logout" }
}
}
}
}
Thanks for your help !!