diff --git a/db/1.sqlite b/db/1.sqlite index d67c736..913c080 100644 Binary files a/db/1.sqlite and b/db/1.sqlite differ diff --git a/db/1.sqlite-shm b/db/1.sqlite-shm index dc07540..fe9ac28 100644 Binary files a/db/1.sqlite-shm and b/db/1.sqlite-shm differ diff --git a/db/1.sqlite-wal b/db/1.sqlite-wal index c35f57f..e69de29 100644 Binary files a/db/1.sqlite-wal and b/db/1.sqlite-wal differ diff --git a/db/auth_copy_2.sqlite-wal b/db/auth_copy_2.sqlite-wal index cfe147f..4ad6bd0 100644 Binary files a/db/auth_copy_2.sqlite-wal and b/db/auth_copy_2.sqlite-wal differ diff --git a/src/utils/auth.rs b/src/utils/auth.rs index 0d115b8..b2240a0 100644 --- a/src/utils/auth.rs +++ b/src/utils/auth.rs @@ -162,21 +162,21 @@ where S: Send + Sync, pub async fn register_user ( State(state): State, RegisterPayload(payload): RegisterPayload -) -> Result { +) -> Result { let hashed_password = hash_password(&payload.password) - .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Password hashing failed"))?; + .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("Password hashing failed: {}", e)))?; let conn = state.logs_pool.get() - .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB connection error"))?; + .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("DB connection error: {}", e)))?; conn.execute( "INSERT INTO users (username, password, displayname) VALUES (?1, ?2, ?3)", params![payload.username, hashed_password, payload.displayname], ) - .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB insert error"))?; - + .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("User insert error: {}", e)))?; + let user_id = conn.last_insert_rowid(); for &hotel_id in &payload.hotel_ids { @@ -186,23 +186,21 @@ pub async fn register_user ( let hotel_name: String = conn .query_row( - "SELECT hotel_name FROM hotels + "SELECT hotelname FROM hotels WHERE id = ?1 ", params![hotel_id], |row| row.get(0), - ).map_err(|_| (StatusCode::BAD_REQUEST, "Invalid hotel ids"))?; - - conn.execute( - "INSERT INTO hotel_user_link (user_id, hotel_id, username, hotel_name) + ).map_err(|e| (StatusCode::BAD_REQUEST, format!("Invalid hotel id {}: {}", hotel_id, e)))?; + + conn.execute( + "INSERT INTO hotel_user_link (user_id, hotel_id, username, hotelname) VALUES (?1, ?2, ?3, ?4)", params![user_id, hotel_id, payload.username, hotel_name], ) - .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Link insert error"))?; - + .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("Link insert error for user_id={} hotel_id={}: {}", user_id, hotel_id, e)))?; } - - Ok((StatusCode::CREATED, "User registered successfully")) + Ok((StatusCode::CREATED, "User registered successfully".to_string())) } #[derive(Serialize, Deserialize, Debug)] @@ -534,35 +532,31 @@ pub async fn create_refresh_token( /*.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Error mapping hotel_ids".to_string())); */ - let mut exist_stmt = conn.prepare( - "SELECT id FROM refresh_token - WHERE device_id = ?1 AND user_agent = ?2 AND user_id=?3" - ) .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?; + + - let existing_token_id: i32 = match exist_stmt.query_one( - params![device_id_str,user_agent_str, user_id], - |row| row.get (0) - ) { - Ok(id) => id, - Err(_) => return Err((StatusCode::INTERNAL_SERVER_ERROR, "error fetching credentials".to_string())), - }; - - match existing_token_id { - //placeholder functions and match arms - Some(id) => updateToken(id), - None(_) => createNewToken(), - - }; - +//FIXME: might not need the hotel list on tconflict ? conn.execute( - "INSERT INTO refresh_token (user_id, token_hash, device_id, user_agent, hotel_id_list) - VALUES (?1, ?2, ?3, ?4, ?5)", + r#" + INSERT INTO refresh_token ( + user_id, + token_hash, + device_id, + user_agent, + hotel_id_list + ) + VALUES (?1, ?2, ?3, ?4, ?5) + ON CONFLICT(user_id, device_id, user_agent) + DO UPDATE SET + token_hash = excluded.token_hash, + hotel_id_list = excluded.hotel_id_list + "#, params![ - &user_id, - &hashed_token, - &device_id_str, - &user_agent_str, - &hotel_ids_json, + user_id, + hashed_token, + device_id_str, + user_agent_str, + hotel_ids_json ], ).map_err(|e| { (StatusCode::INTERNAL_SERVER_ERROR, format!("DB error: {}", e))