diff --git a/backend/src/online_classroom/session.rs b/backend/src/online_classroom/session.rs index 3e8f0aa..f1b0e74 100644 --- a/backend/src/online_classroom/session.rs +++ b/backend/src/online_classroom/session.rs @@ -42,16 +42,7 @@ pub async fn request(url: String) -> Result { match response.text() { Ok(t) => { - // Get current time and date in iso - let now: DateTime = Local::now(); - let now = now.to_rfc3339(); - - // Write html to file - let r = std::fs::write(format!("request-logs/{}.html", now), &t); - if let Err(err) = r { - eprintln!("Error writing request html to file: {:?}", err) - } - + log_html(&t); Ok(t) } Err(err) => Err(format!("Error getting text from response: {:?}", err)), @@ -95,16 +86,7 @@ pub async fn create_user_request(url: String, body: String) -> Result { - // Get current time and date in iso - let now: DateTime = Local::now(); - let now = now.to_rfc3339(); - - // Write html to file - let r = std::fs::write(format!("request-logs/{}.html", now), &t); - if let Err(err) = r { - eprintln!("Error writing request html to file: {:?}", err) - } - + log_html(&t); Ok(t) } Err(err) => Err(format!("Error getting text from response: {:?}", err)), @@ -134,7 +116,10 @@ pub async fn register_courses_request(url: String, body: String) -> Result Ok(t), + Ok(t) => { + log_html(&t); + Ok(t) + }, Err(err) => Err(format!("Error getting text from response: {:?}", err)), } } @@ -172,27 +157,31 @@ async fn login() -> Result<(), String> { let jar = CookieJar::new(); + let login_body = format!( + "login={}&password={}&submitAuth=&_qf__formLogin=", + encode(classroom_user.as_str()).into_owned(), + encode(classroom_password.as_str()).into_owned(), + ); + let response = Request::post(format!("{}/index.php", classroom_url)) .header("Content-Type", "application/x-www-form-urlencoded") .cookie_jar(jar.clone()) - .body(format!( - "login={}&password={}&submitAuth=&_qf__formLogin=", - encode(classroom_user.as_str()), - encode(classroom_password.as_str()), - )) + .body(login_body) .unwrap() .send(); match response { Ok(mut r) => { if r.status() == isahc::http::StatusCode::FOUND { + // TODO: Even if this is a 302, it might not be a successful login + // check Set-Cookie header SESSION_COOKIE.write().unwrap().jar = jar.clone(); Ok(()) } else { // Write html to file match r.text() { Ok(t) => { - log_html(t); + log_html(&t); } Err(err) => { return Err(format!("Error getting text from login response: {:?}", err)) @@ -206,13 +195,13 @@ async fn login() -> Result<(), String> { } } -fn log_html(html: String) { +fn log_html(html: &String) { // Get current time and date in iso let now: DateTime = Local::now(); let now = now.to_rfc3339(); // Write html to file - let r = std::fs::write(format!("request-logs/{}.html", now), &html); + let r = std::fs::write(format!("request-logs/{}.html", now), html); if let Err(err) = r { eprintln!("Error writing request html to file: {:?}", err) } diff --git a/frontend/src/OnlineClassroom/ClassroomVinculation.tsx b/frontend/src/OnlineClassroom/ClassroomVinculation.tsx index 1417610..3e1ba07 100644 --- a/frontend/src/OnlineClassroom/ClassroomVinculation.tsx +++ b/frontend/src/OnlineClassroom/ClassroomVinculation.tsx @@ -29,12 +29,12 @@ export function ClassroomVinculation(props: { setClassroomUsers([]); setStatus(LoadingStatus.Error); console.error(response.data); - setError(response.data.Err.reason); + setError(response.data.Error.reason); } }) .catch((err: AxiosError>>) => { console.error(err); - setError(`Error: ${err.response?.data.Err.reason ?? err.message}`); + setError(`Error: ${err.response?.data?.Error?.reason ?? err.message}`); setStatus(LoadingStatus.Error); }); }; @@ -112,9 +112,9 @@ function ClassroomSingleUser(props: { setStatus(LoadingStatus.Ok); props.onLink(parseInt(props.user.user_id, 10), props.user.username); } else { - console.error(response.data); - setError(response.data.Err.reason); setStatus(LoadingStatus.Error); + console.error(response.data); + setError(response.data.Error.reason); } }) .catch((err) => { diff --git a/frontend/src/types/JsonResult.ts b/frontend/src/types/JsonResult.ts index 553f6c5..4344d57 100644 --- a/frontend/src/types/JsonResult.ts +++ b/frontend/src/types/JsonResult.ts @@ -1,6 +1,6 @@ export type JsonResult = { Ok: T, - Err: { + Error: { reason: string } };