imdrasil / jennifer.cr

Crystal ORM using ActiveRecord pattern with flexible query DSL
MIT License
418 stars 55 forks source link

Password digest not created when using with_authentication directly via model build #419

Closed fdocr closed 2 years ago

fdocr commented 2 years ago

When using with_authentication I see validations errors raised as I build and save the model from params because the digest is empty. I'm guessing this is generated via custom assign of password method in superclass(es).

# Sample Kemal pseudocode
payload = {
    username: env.params.body["username"].as(String),
    email: env.params.body["email"].as(String),
    password: env.params.body["password"].as(String),
    password_confirmation: env.params.body["password_confirmation"].as(String),
  }
user = User.build(payload)
user.save
# Validation errror raised => [\"Password can't be blank\"]

I have to manually assign the password + confirmation fields directly for this to work:

# Sample Kemal pseudocode
payload = {
  username: env.params.body["username"].as(String),
  email: env.params.body["email"].as(String),
}
user = User.build(payload)
user.password = env.params.body["password"].as(String)
user.password_confirmation = env.params.body["password_confirmation"].as(String)
user.save
# Record persisted successfully

Is this the intended behavior/style that should be used? Could accepting this form of record build+save be a feature request you would consider desirable for the project?

If there's a better or suggested approach (thinking ahead of password updating that I haven't worked on yet) I'd love to hear/read about them 🙂

imdrasil commented 2 years ago

yeah, I see how this could be inconvenient. Technically this was done intentionally but I'll address this to make life easier

imdrasil commented 2 years ago

actually no - this is just a bug