Browse Source

Factor out email validtion into a seperate function.

Lee Morgan 1 năm trước cách đây
mục cha
commit
8b1bff8f68
1 tập tin đã thay đổi với 11 bổ sung3 xóa
  1. 11 3
      src/controllers/user.rs

+ 11 - 3
src/controllers/user.rs

@@ -12,9 +12,7 @@ pub async fn create(
 ) -> impl Responder {
     let user_collection = db.collection::<User>("users");
     let email = payload.email.to_lowercase();
-    let email_regex: Regex = Regex::new(r#"^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}(\.[0-9]{1,3}){3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$"#).unwrap();
-
-    if !email_regex.is_match(&email) {
+    if !valid_email(&email) {
         return http_error(StatusCode::BAD_REQUEST, "Invalid email");
     }
 
@@ -65,6 +63,16 @@ pub async fn login(
         .json(response_user(user))
 }
 
+fn valid_email(email: &str) -> bool {
+    let email_regex: Regex = Regex::new(r#"^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}(\.[0-9]{1,3}){3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$"#).unwrap();
+
+    if email_regex.is_match(email) {
+        return true;
+    }
+
+    false
+}
+
 fn response_user(user: User) -> dto::user::ResponseUser {
     dto::user::ResponseUser {
         id: user._id.unwrap().to_string(),