원격 인증 서비스를 통해 사용자를 인증하려고합니다. 나는 서비스에 메시지를 전송하고 결과를 기다리고위한 도우미 메서드를 작성했습니다 : 메시지를 보낼 수없는 경우재생 2 양식 제한
는def authenticateAwait(email: String,
password: String
): Either[String, Option[User]] = {
try {
val future = authenticate(email, password)
Right(Await.result(future, timeout.duration))
} catch {
case _ ⇒ Left("Unable to connect to authentication server")
}
}
는 오류 설명과 함께 Left[String]
을 반환하거나 응답이 없습니다. 서비스 응답을 받으면 Right[Option[User]]
을 반환합니다. 서비스는 인증 결과에 따라 Option[User]
으로 응답합니다.
val loginForm = Form(
tuple(
"email" → email,
"password" → nonEmptyText
) verifying ("Invalid email or password", result => result match {
case (email, password) ⇒
User.authenticateAwait(email, password) match {
case Left(_) ⇒ true
case Right(optUser) ⇒ optUser.isDefined
}
}) verifying ("Unable to connect to authentication server", result => result match {
case (email, password) ⇒
User.authenticateAwait(email, password) match {
case Left(_) ⇒ false
case Right(optUser) ⇒ true
}
})
)
것은이 코드에 대해 나를 걱정이 두 번 authenticateAwait
를 호출합니다. 이는 하나의 유효성 검증 당 정확히 두 개의 메시지가 전송된다는 것을 의미합니다. 실제로 필요한 것은 authenticateAwait
으로 한 번 호출하여 결과를 저장하고 다양한 유효성 검사를 수행하는 것입니다. 간단한 해결책이없는 것 같습니다.
오류 따라서 I가 검증에 인증을 수행한다에만 작성 중에 형태로 부착 될 수있다, 그러나, 상기 문제가 발생한다.
내가와 함께 임시 해결 방법과 내부 var
을 정의하는 것입니다.
def loginForm = {
var authResponse: Either[String, Option[commons.User]] = null
Form(
tuple(
"email" → email,
"password" → nonEmptyText
) verifying ("Invalid email or password", result ⇒ result match {
case (email, password) ⇒
authResponse = User.authenticateAwait(email, password)
authResponse match {
case Left(_) ⇒ true
case Right(optUser) ⇒ optUser.isDefined
}
}) verifying ("Unable to connect to authentication server", result ⇒ result match {
case (email, password) ⇒
authResponse match {
case Left(_) ⇒ false
case Right(optUser) ⇒ true
}
})
)
}
이것은 분명히 해킹입니다. 더 나은 해결책이 있습니까?
업데이트 : 제 생각에 양식은 입력을 위생적으로 처리해야하지만 양식은 나중에 양식 외부에서 수행해야합니다. 문제는 오류가 Form
의 일부로보기로 전송되며 오류를 기존 양식에 첨부 할 수 없다는 것입니다. 오류가있는 새로운 양식을 만드는 간단한 방법은 없습니다.
AJAX라고합니다. 그것을 사용하면 존재하지 않는 문제를 해결하기 위해 ginormous code blocks을 만들지 않을 것입니다 (힌트 : 새로운 양식을 만들 필요가 없습니다) – virtualeyes