Asynchronously creates a user with a password and saves it to the database. # Errors When could not save the user into the DB
(
db: &DatabaseConnection,
params: &RegisterParams,
)
| 156 | /// |
| 157 | /// When could not save the user into the DB |
| 158 | pub async fn create_with_password( |
| 159 | db: &DatabaseConnection, |
| 160 | params: &RegisterParams, |
| 161 | ) -> ModelResult<Self> { |
| 162 | let txn = db.begin().await?; |
| 163 | |
| 164 | if users::Entity::find() |
| 165 | .filter(users::Column::Email.eq(¶ms.email)) |
| 166 | .one(&txn) |
| 167 | .await? |
| 168 | .is_some() |
| 169 | { |
| 170 | return Err(ModelError::EntityAlreadyExists {}); |
| 171 | } |
| 172 | |
| 173 | let password_hash = |
| 174 | hash::hash_password(¶ms.password).map_err(|e| ModelError::Any(e.into()))?; |
| 175 | let user = users::ActiveModel { |
| 176 | email: ActiveValue::set(params.email.to_string()), |
| 177 | password: ActiveValue::set(password_hash), |
| 178 | name: ActiveValue::set(params.name.to_string()), |
| 179 | ..Default::default() |
| 180 | } |
| 181 | .insert(&txn) |
| 182 | .await?; |
| 183 | |
| 184 | txn.commit().await?; |
| 185 | |
| 186 | Ok(user) |
| 187 | } |
| 188 | |
| 189 | /// Creates a JWT |
| 190 | /// |