windweller
windweller

Reputation: 2385

Play Scala Form type mismatch between Option and Unit

This is really weird. I don't know what has gone wrong, and have tried to fix this for at least an hour. I wrote an ad-hoc validator for the form:

def validate (email: String, password: String): Option[UserData] = DB.withSession { implicit rs: scala.slick.session.Session =>
    val result = Users.get(email)

    if (result.isEmpty) None
    else if(password.bcrypt hash= result(0).password) {
      Some(UserData(email, password))
    }
   }

Full disclosure, I used play-slick. Also the mysterious password.bcrypt hash= ... is a encryption function I borrowed Hasher: https://github.com/Nycto/Hasher. I defined UserData within the same controller:

case class UserData(email: String, password: String)

This code seems all fine with me, but the complier from play console said:

 /Users/.../Desktop/blog/play-slick-blog/app/controllers/Login.scala:25:
 type mismatch; found : Unit required:
 Option[controllers.Login.UserData] 
 else if(password.bcrypt hash= result(0).password) {
  ^ one error found

This is not supposed to be an error right??? What's going on?

One additional question I need to ask: in my Login page view, the first line looks like this: @(form: Form[Login.UserData]). Then the program asks me to pass in a form value each time I call render(). The problem is, I don't have a form value, if the user first logs in. What should I pass in then??

Thank you for helping!

Upvotes: 0

Views: 245

Answers (1)

Ren
Ren

Reputation: 3455

Its probably because your function will return Unit in the case where the first and second ifs are false.

Try

if(!result.isEmpty && password.bcrypt hash= result(0).password) {
  Some(UserData(email, password))
}
else {
   None
}

To see if it resolves your issue.

Upvotes: 2

Related Questions