From a53ccc6f5ca64d46abb94ae743dc5d063a87959e Mon Sep 17 00:00:00 2001
From: Yigit Sever
Date: Tue, 20 Apr 2021 00:43:21 +0300
Subject: Bugfix

Users cannot authenticate twice now
---
 src/handlers.rs | 12 ++++++------
 src/schema.rs   |  6 ++++++
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/src/handlers.rs b/src/handlers.rs
index e12d83e..6305560 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -229,8 +229,6 @@ pub async fn authenticate_user(
         }
     };
 
-    let provided_id = request.student_id.clone();
-
     let privileged_student_id = match MetuId::new(request.student_id, request.passwd) {
         Some(id) => id,
         None => {
@@ -243,18 +241,20 @@ pub async fn authenticate_user(
         }
     };
 
+    // Students should be able to authenticate once
     {
         let userlist = db.users.read();
 
-        if userlist.contains_key(&provided_id) {
-            let res_json = warp::reply::json(&GradeCoinResponse {
+        for (_, user) in userlist.iter() {
+            if user.user_id == privileged_student_id {
+                let res_json = warp::reply::json(&GradeCoinResponse {
                 res: ResponseType::Error,
                 message:
                     "This user is already authenticated, do you think this is a mistake? Contact me"
                         .to_owned(),
             });
-
-            return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST));
+                return Ok(warp::reply::with_status(res_json, StatusCode::BAD_REQUEST));
+            }
         }
     }
 
diff --git a/src/schema.rs b/src/schema.rs
index 81bfc1b..40c6329 100644
--- a/src/schema.rs
+++ b/src/schema.rs
@@ -253,6 +253,12 @@ pub struct MetuId {
     passwd: String,
 }
 
+impl MetuId {
+    pub fn quick_equal(&self, other: &str) -> bool {
+        self.id == other
+    }
+}
+
 /// The plaintext of the initial user authentication request
 #[derive(Serialize, Deserialize, Debug, PartialEq)]
 pub struct AuthRequest {
-- 
cgit v1.2.3-70-g09d2