Damian Lall
09/09/2019, 2:22 PMget
handler inside an authenticate
block, I'll be prompted for credentials when I try to access the route, so once the get
handler starts being executed, I can access the UserIdPrincipal
and start finding data associated with the account. However, I now want to expand my authenticate
block to include multiple routes, so I thought I could handle the initial processing of the principal/account inside an intercept
block. Yet, when I try to do this, I am not prompted for my credentials and therefore the UserIdPrincipal
inside the interceptor is null. How can I get Ktor to prompt me for my credentials from within a route interceptor inside an authenticate
block?Damian Lall
09/09/2019, 2:23 PMrouting {
authenticate("teacherAuth") {
get("dashboard") {
val principal = call.principal<UserIdPrincipal>()!!
val schoolName = principal.name
val school = transaction {
School.find { Schools.name eq schoolName }.singleOrNull()
}
if (school == null)
call.respondText("No school \"$schoolName\" found")
else
call.respondHtml {
...
}
}
}
}
Damian Lall
09/09/2019, 2:23 PMauthenticate("teacherAuth") {
val schoolKey = AttributeKey<School>("school")
intercept(ApplicationCallPipeline.Setup) {
val principal = call.principal<UserIdPrincipal>()!!
val schoolName = principal.name
val school = transaction {
School.find { Schools.name eq schoolName }.singleOrNull()
}
if (school == null) {
call.respondText("No school \"$schoolName\" found")
return@intercept finish()
}
call.attributes.put(schoolKey, school)
}
get("dashboard") {
val school = call.attributes[schoolKey]
call.respondHtml {
...
}
}
}
Sergey Akhapkin
09/10/2019, 8:17 AMDamian Lall
09/10/2019, 2:39 PMSergey Akhapkin
09/10/2019, 7:45 PM