在 Vapor 中,我可以通过以下方式轻松保护登录会话中的路由:
drop.group(protect) {
secure in
secure.get("secureRoute", handler: )
secure.post("securePostRoute", handler: )
//and so forth
}
处理程序照常进行,不检查会话,因为它已经由drop.group(protect)
.
但是,在 Kitura 中,似乎我想实现同样的目标,我必须这样做:
router.get("/") {
request, response, next in
//Get the current session
sess = request.session
//Check if we have a session and it has a value for email
if let sess = sess, let email = sess["email"].string {
try response.send(fileName: pathToFile).end()
} else {
try response.send(fileName: pathToAnotherFile).end()
}
}
我必须手动检查每个安全路由中的会话。这最终会变得非常多余。
有没有像 Vapor 一样优雅的解决方案?