Log login html response

This commit is contained in:
Araozu 2023-10-05 12:50:29 -05:00
parent 16bd38e6a7
commit 47f20b8920
2 changed files with 56 additions and 35 deletions

View File

@ -1,7 +1,7 @@
use chrono::{DateTime, Local, TimeZone, Utc};
use lazy_static::lazy_static;
use urlencoding::encode;
use std::time::{SystemTime, UNIX_EPOCH};
use urlencoding::encode;
use isahc::{cookies::CookieJar, prelude::*, Request};
use std::sync::RwLock;
@ -106,7 +106,7 @@ pub async fn create_user_request(url: String, body: String) -> Result<String, St
}
Ok(t)
},
}
Err(err) => Err(format!("Error getting text from response: {:?}", err)),
}
}
@ -134,19 +134,7 @@ pub async fn register_courses_request(url: String, body: String) -> Result<Strin
};
match response.text() {
Ok(t) => {
// Get current time and date in iso
let now: DateTime<Local> = 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)
}
Ok(t)
},
Ok(t) => Ok(t),
Err(err) => Err(format!("Error getting text from response: {:?}", err)),
}
}
@ -196,10 +184,36 @@ async fn login() -> Result<(), String> {
.send();
match response {
Ok(_) => {
SESSION_COOKIE.write().unwrap().jar = jar.clone();
Ok(())
Ok(mut r) => {
if r.status() == isahc::http::StatusCode::FOUND {
SESSION_COOKIE.write().unwrap().jar = jar.clone();
Ok(())
} else {
// Write html to file
match r.text() {
Ok(t) => {
log_html(t);
}
Err(err) => {
return Err(format!("Error getting text from login response: {:?}", err))
}
};
Err(format!("Error connecting to online classroom: not 302"))
}
}
Err(error) => Err(format!("Error connecting to online classroom: {:?}", error)),
}
}
fn log_html(html: String) {
// Get current time and date in iso
let now: DateTime<Local> = Local::now();
let now = now.to_rfc3339();
// Write html to file
let r = std::fs::write(format!("request-logs/{}.html", now), &html);
if let Err(err) = r {
eprintln!("Error writing request html to file: {:?}", err)
}
}

View File

@ -6,30 +6,37 @@ import { LoadingStatus, backend, useLoading, wait } from "../utils/functions";
import { LinkIcon } from "../icons/LinkIcon";
import { For, Show, createSignal, onMount } from "solid-js";
import { LoadingIcon } from "../icons/LoadingIcon";
import { AxiosError } from "axios";
type Status = "Init" | "Ok" | "Loading" | "Error";
export function ClassroomVinculation(props: {
person_surname: string,
personId: number,
onLink: (classroom_id: number, classroom_username: string) => void,
}) {
const [classroomUsers, setClassroomUsers] = createSignal<ClassroomRegistrationUser[]>([]);
const [error, setError] = createSignal("");
const [status, setStatus] = createSignal<Status>("Init");
const {status, setStatus, error, setError} = useLoading();
const loadUsers = async() => {
setStatus("Loading");
const response = await fetch(`${import.meta.env.VITE_BACKEND_URL}/api/classroom/users/${encodeURIComponent(props.person_surname)}`);
const json: JsonResult<Array<ClassroomRegistrationUser>> = await response.json();
setClassroomUsers([]);
setStatus(LoadingStatus.Loading);
if (response.ok) {
setClassroomUsers(json.Ok);
setStatus("Ok");
} else {
console.error("Error loading users", json);
setError(json.Err.reason);
setStatus("Error");
}
backend.get<JsonResult<Array<ClassroomRegistrationUser>>>(`/api/classroom/users/${encodeURIComponent(props.person_surname)}`)
.then((response) => {
if (response.status === 200) {
setClassroomUsers(response.data.Ok);
setStatus(LoadingStatus.Ok);
} else {
setClassroomUsers([]);
setStatus(LoadingStatus.Error);
console.error(response.data);
setError(response.data.Err.reason);
}
})
.catch((err: AxiosError<JsonResult<Array<ClassroomRegistrationUser>>>) => {
console.error(err);
setError(`Error: ${err.response?.data.Err.reason ?? err.message}`);
setStatus(LoadingStatus.Error);
});
};
onMount(loadUsers);
@ -40,13 +47,13 @@ export function ClassroomVinculation(props: {
Vincule un usuario existente:
</p>
<Show when={status() === "Loading"}>
<Show when={status() === LoadingStatus.Loading}>
<div class="text-center h-12 scale-150">
<LoadingIcon class="animate-spin" fill="var(--c-primary)" />
</div>
</Show>
<Show when={status() === "Ok"}>
<Show when={status() === LoadingStatus.Ok}>
<Show when={classroomUsers().length === 0}>
<div class="px-4 pb-2">
<div class="text-center pt-6 pb-4">
@ -66,7 +73,7 @@ export function ClassroomVinculation(props: {
</For>
</Show>
<Show when={status() === "Error"}>
<Show when={status() === LoadingStatus.Error}>
<div class="px-4 pb-2 text-c-error">
<div class="text-center pt-6 pb-4">
<XcircleIcon class="scale-[200%]" fill="var(--c-error)" />