Hello i've this code which simply updates user data in the DB but i'm not sure if this is idiomatic rust, i used 3 match statements is there any way to write this a better way? I also have 3 unwrap() s left so that's another 3 match statements.
So this api does these checks:
-Check if the JWT token is valid
-Check if the user exists
-Check if the hashed password inside the JWT token equals to the hashed password inside DB, This is to check if the password is changed,
-Check current password, if it's true proceed to updated the user with the new data and return a new JWT token.
So is there any better way to write this code?
match auth_token {
Some(t) => {
let token = t.to_str().unwrap();
let decoded_token = decode_jwt(token).unwrap();
let db = connect().await.unwrap();
let users_collection = db.collection::<User>("users");
let user_exists =
find_document(&users_collection, doc! {"user_id":&decoded_token.user_id})
.await
.unwrap();
// CHECK if user_exists
match user_exists {
Some(u) => {
// CHECK Token password
if u.password != decoded_token.password {
Ok(HttpResponse::Unauthorized().body("Password Changed"))
} else {
// CHECK password
let correct_password = check_password(&u.password, &user.password);
match correct_password {
true => {
// Update user
let update_user = UpdateUser {
password: hash_password(user.new_password.clone()),
country: user.country.clone(),
phone_number: user.phone_number.clone(),
};
let document = bson::to_document(&update_user).unwrap();
let update_result = update_document(
&users_collection,
doc! {"user_id":&u.user_id},
doc! {"$set":document},
)
.await
.unwrap()
.unwrap();
let token = Token {
user_id: u.user_id,
password: hash_password(Some(update_result.password)).unwrap(),
iat: chrono::Utc::now().timestamp_millis() as u32,
exp: chrono::Utc::now().timestamp_millis() as u32 + 999999,
};
let token = create_jwt(token).unwrap();
Ok(HttpResponse::Ok().body(token))
}
false => Ok(HttpResponse::Unauthorized().body("Incorrect Password")),
}
}
}
None => Ok(HttpResponse::Forbidden().body("Invalid email")),
}
}
None => Ok(HttpResponse::Forbidden().body("Invalid token")),
}