0
votes

I am doing a simple select query to the database for a login system. I am supposed to get a record from the database and then compare passwords but I am struggling getting that part to work mainly on getting the select query to return what I need. This is what I have

 def example() = Action.async {

    val myquery=sql"""SELECT password,id,email from profiles where [email protected]""".as[(String,Int,String)]


    dbcall.run(myquery.map { Result =>

      Result.headOption match {
        case d: (String, Int,String) =>
          if (BCrypt.checkpw("mypassword", d._1)) {
            Ok("it worked")
          }
          else
            Ok("No match")

        case None => Ok("Empty")
      }
    }

  }

This code works but it is not returning back in the correct format, it is giving me an error stating:

[MatchError: Some(($2a$12$HoNBl1DBlH.X,32,[email protected])) (of class scala.Some)]

Notice above that is the correct values that I want, however I want to strictly get the first column: password which I thought I did here...

BCrypt.checkpw("mypassword", d._1)

However I just keep getting a match exception error on Result.headOption match, isn't d._1 suppose to only give the `password column value?

1

1 Answers

2
votes

Result.headOption is returning an Option[(String, Int, String)], but you're matching on (String, Int, String). You could try something like this, instead:

 Result.headOption match {
    case Some((password, _, _)) =>
      if (BCrypt.checkpw("mypassword", password)) {
        Ok("it worked")
      } else {
        Ok("No match")
      }

    case None => Ok("Empty")
  }
}

In case Some((password, _, _)), we call the first member of the tuple password, and we use the wildcard _ to essentially ignore the second and third members of the tuple. Then, on the right hand side, we can refer to password instead of d._1.