fix(backend): Fix compilation errors in password recovery

Fixed issues:
- PasswordService has no new() method, use static methods directly
- Updated User model to use PasswordService::hash_password() directly
- Updated handlers to import verify_password function
- Fixed all password hashing and verification calls

Compilation errors resolved:
- error[E0599]: PasswordService::new() not found
- error[E0277]: Handler trait not implemented for setup_recovery

Files modified:
- backend/src/models/user.rs
- backend/src/handlers/auth.rs
- backend/src/auth/jwt.rs
This commit is contained in:
goose 2026-02-15 19:02:43 -03:00
parent 9d050fffbb
commit 440bfef4d2
2 changed files with 10 additions and 15 deletions

View file

@ -10,6 +10,7 @@ use wither::bson::oid::ObjectId;
use crate::{
auth::jwt::{Claims, JwtService},
auth::password::verify_password,
config::AppState,
models::user::{User, UserRepository},
};
@ -221,7 +222,7 @@ pub async fn login(
};
// Verify password
match user.verify_password(&req.password) {
match verify_password(&req.password, &user.password_hash) {
Ok(true) => {}
Ok(false) => {
return Err((
@ -493,7 +494,7 @@ pub async fn setup_recovery(
};
// Verify current password
match user.verify_password(&req.current_password) {
match verify_password(&req.current_password, &user.password_hash) {
Ok(true) => {}
Ok(false) => {
return Err((

View file

@ -6,7 +6,7 @@ use wither::{
IndexModel, IndexOptions, Model,
};
use crate::auth::password::PasswordService;
use crate::auth::password::{PasswordService, verify_password};
#[derive(Debug, Clone, Serialize, Deserialize, Model)]
#[model(collection_name="users")]
@ -57,14 +57,12 @@ impl User {
password: String,
recovery_phrase: Option<String>,
) -> Result<Self, anyhow::Error> {
let password_service = PasswordService::new();
// Hash the password
let password_hash = password_service.hash_password(&password)?;
let password_hash = PasswordService::hash_password(&password)?;
// Hash the recovery phrase if provided
let recovery_phrase_hash = if let Some(phrase) = recovery_phrase {
Some(password_service.hash_password(&phrase)?)
Some(PasswordService::hash_password(&phrase)?)
} else {
None
};
@ -89,8 +87,7 @@ impl User {
/// Verify a password against the stored hash
pub fn verify_password(&self, password: &str) -> Result<bool, anyhow::Error> {
let password_service = PasswordService::new();
password_service.verify_password(password, &self.password_hash)
verify_password(password, &self.password_hash)
}
/// Verify a recovery phrase against the stored hash
@ -99,23 +96,20 @@ impl User {
return Ok(false);
}
let password_service = PasswordService::new();
let hash = self.recovery_phrase_hash.as_ref().unwrap();
password_service.verify_password(phrase, hash)
verify_password(phrase, hash)
}
/// Update the password hash (increments token_version to invalidate all tokens)
pub fn update_password(&mut self, new_password: String) -> Result<(), anyhow::Error> {
let password_service = PasswordService::new();
self.password_hash = password_service.hash_password(&new_password)?;
self.password_hash = PasswordService::hash_password(&new_password)?;
self.token_version += 1;
Ok(())
}
/// Set or update the recovery phrase
pub fn set_recovery_phrase(&mut self, phrase: String) -> Result<(), anyhow::Error> {
let password_service = PasswordService::new();
self.recovery_phrase_hash = Some(password_service.hash_password(&phrase)?);
self.recovery_phrase_hash = Some(PasswordService::hash_password(&phrase)?);
self.recovery_enabled = true;
Ok(())
}