2

How do I select on an encrypted variable with Slick.

I have a BCrypt encoded password in my database.

To Illustrate my intentions:

def login(name: String, password: String) = Action {
...
  for {
    u <- Users if u.name === name && BCrypt.checkpw(password, u.password)
  } yield u

Of course slick complains about u.password being a lifted column and not a String.

How would you go about to solve the problem?

4

2 回答 2

3

实际上我设法解决了我的问题。

  def login(name: String, password: String) = Action {
    database withSession {
      (for {
        u <- Users if u.name === name 
      } yield u).list
    } match {
      case Nil => Ok("No user found")
      case head :: tail => 
        if(BCrypt.checkpw(password, head.password))
          Ok("accepted").withSession("userid" -> head.id.get.toString)
        else
          Ok("Incorrect password")
    }
  }
于 2013-02-06T12:59:49.020 回答
0

我有同样的问题,但在模型课上。

由于您的解决方案激发了我的灵感,因此我将其发布在这里,希望对您有所帮助。

def authenticate(email: String, password: String): Future[Option[User]] = db.run {
    Users.filter{_.email === email}.result.headOption
  } map {
    case Some(u) if BCrypt.checkpw(password, u.password) => Some(u)
    case _ => None
  }
于 2015-11-04T11:40:28.660 回答