Maverick
Maverick

Reputation: 2760

Play framework scala form object for password field binding?

I have models as follows:

case class User(uid: Option[Int], email: String, password: String, created_at: Timestamp, updated_at: Timestamp)

case class UserProfile(firstname: String, lastname: String, gender: Int, user_id: Long)

And I defined the form binding as follows:

    val date = new Date()
  val currentTimestamp= new Timestamp(date.getTime());
  val registerForm = Form(
    tuple(
          "user" -> mapping(
            "uid" -> optional(number),
            "email" -> email,
            "password" -> nonEmptyText,
            "created_at" -> ignored(currentTimestamp),
          "updated_at" -> ignored(currentTimestamp)
        )  (User.apply)(User.unapply),
        "profile" -> mapping(
          "firstname"->nonEmptyText,
          "lastname"->nonEmptyText,
          "gender" -> ignored(0),
          "user_id" -> ignored(0L)
        )(UserProfile.apply)(UserProfile.unapply))
    )

Now, I want to store the password using hashing before it gets saved/inserted in db using slick. I could try and do it by creating a new User object but that doesnt sound like efficient method.

Any other approaches?

Thanks in advance

------------------------------------------ EDIT 1 ---------------------------------------------------------- This is my insert logic with slick:

def insert(user: User): Future[Any] = {
    println("coming inside insert of user dao")
    println(user)
//    insertUP(user)
    db.run((Users returning Users.map(_.uid)) += user)
  }

Upvotes: 1

Views: 334

Answers (1)

vdebergue
vdebergue

Reputation: 2414

How about something like:

def insert(user: User): Future[Any] = {
  val hashedPassword = hashPassword(user.password)
  val updatedUser = user.copy(password = hashedPassword)
  db.run((Users returning Users.map(_.uid)) += updatedUser)
}

Upvotes: 1

Related Questions