Add Login Error Text Display and Logout callback
Some checks failed
Run Tests on Code / run-tests (push) Failing after 1m4s
Build binary file and bundle packages / test (pull_request) Failing after 1m3s
Build binary file and bundle packages / build (pull_request) Has been skipped

This commit is contained in:
Neshura 2024-01-13 16:16:34 +01:00
parent 9a1ee2ba63
commit 53e6f72cbb
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 28 additions and 17 deletions

View file

@ -24,7 +24,7 @@ pub(crate) struct State {
} }
impl State { impl State {
pub(crate) async fn login(&self) -> LoginState { pub(crate) async fn login() -> LoginState {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let res = match client.get(api!() + "app/v1/auth/otp4app/generate?format=json") let res = match client.get(api!() + "app/v1/auth/otp4app/generate?format=json")
@ -49,7 +49,15 @@ impl State {
} }
} }
pub(crate) async fn load_library(&self) -> LibraryState { pub(crate) async fn logout(state: &Self) {
let client = reqwest::Client::new();
let url = api!() + format!("app/v1/auth/logout?{:?}", state.token).as_str();
let _ = client.post(url).send().await;
}
pub(crate) async fn load_library(state: State) -> LibraryState {
/* /*
1: lib = https://labs.j-novel.club/app/v1/me/library 1: lib = https://labs.j-novel.club/app/v1/me/library
2: every vol in lib.books serie = https://labs.j-novel.club/app/v1/volumes/{vol.volume.legacyId}/serie?format=json 2: every vol in lib.books serie = https://labs.j-novel.club/app/v1/volumes/{vol.volume.legacyId}/serie?format=json
@ -58,7 +66,9 @@ impl State {
*/ */
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let url = api!() + format!("app/v1/me/library?format=json&{:?}", self.token).as_str(); let url = api!() + format!("app/v1/me/library?format=json&{:?}", state.token).as_str();
println!("{url}");
let res = match client.get(url) let res = match client.get(url)
.send() .send()
@ -68,9 +78,9 @@ impl State {
}; };
match res.text().await { match res.text().await {
Ok(otp_data) => { Ok(text) => {
let otp = match serde_json::from_str(&otp_data) { let library = match serde_json::from_str(&text) {
Ok(otp) => otp, Ok(library) => library,
Err(e) => return LibraryState::Error(e.to_string()), Err(e) => return LibraryState::Error(e.to_string()),
}; };
@ -144,11 +154,11 @@ impl Otp {
} }
} }
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
pub(crate) struct OtpToken { pub(crate) struct OtpToken {
pub(crate) id: ArrayString<36>, pub(crate) id: ArrayString<36>,
pub(crate) ttl: ArrayString<16>, pub(crate) ttl: String,
pub(crate) created: ArrayString<16> pub(crate) created: String
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]

View file

@ -9,8 +9,6 @@ use chrono::{DateTime, Local, Utc};
use reqwest::StatusCode; use reqwest::StatusCode;
use serde::{Deserialize}; use serde::{Deserialize};
use crate::Menu::JNovel;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct CalibreWebImporter { struct CalibreWebImporter {
jnc: jnovel::State, jnc: jnovel::State,
@ -24,8 +22,8 @@ impl Default for CalibreWebImporter {
Self { Self {
jnc: jnovel::State::default(), jnc: jnovel::State::default(),
settings: AppSettings::default(), settings: AppSettings::default(),
current_menu: JNovel, current_menu: Menu::JNovel,
previous_menu: JNovel, previous_menu: Menu::JNovel,
} }
} }
} }
@ -117,13 +115,13 @@ impl Application for CalibreWebImporter {
jnovel::Message::Login => { jnovel::Message::Login => {
self.jnc.login_state = jnovel::LoginState::LoggingIn; self.jnc.login_state = jnovel::LoginState::LoggingIn;
let _ = open::that("https://j-novel.club/user/otp"); let _ = open::that("https://j-novel.club/user/otp");
Command::perform(self.jnc.login(), |status| Message::JncAction(jnovel::Message::LoginState(status))) Command::perform(jnovel::State::login(), |status| Message::JncAction(jnovel::Message::LoginState(status)))
} }
jnovel::Message::Logout => { jnovel::Message::Logout => {
println!("Logging out from J-Novel Club"); println!("Logging out from J-Novel Club");
self.jnc.token = None; self.jnc.token = None;
self.jnc.login_state = jnovel::LoginState::LoggedOut; self.jnc.login_state = jnovel::LoginState::LoggedOut;
Command::none() Command::perform(jnovel::State::logout(self.jnc.clone()), || Message::JncAction(jnovel::Message::LoginState(jnovel::LoginState::LoggedOut)))
} }
jnovel::Message::LoginState(status) => { jnovel::Message::LoginState(status) => {
if let jnovel::LoginState::Success(token) = status { if let jnovel::LoginState::Success(token) = status {
@ -131,7 +129,7 @@ impl Application for CalibreWebImporter {
self.jnc.login_state = status; self.jnc.login_state = status;
if self.jnc.library_state == jnovel::LibraryState::Unloaded { if self.jnc.library_state == jnovel::LibraryState::Unloaded {
Command::perform(self.jnc.load_library(), |status| Message::JncAction(jnovel::Message::LibraryState(status))) Command::perform(jnovel::State::load_library(self.jnc.clone()), |status| Message::JncAction(jnovel::Message::LibraryState(status)))
} }
else { else {
Command::none() Command::none()
@ -190,7 +188,10 @@ impl Application for CalibreWebImporter {
jnovel::LoginState::LoggedOut => Element::from(button("Login").on_press(Message::JncAction(jnovel::Message::Login))), jnovel::LoginState::LoggedOut => Element::from(button("Login").on_press(Message::JncAction(jnovel::Message::Login))),
jnovel::LoginState::LoggingIn => Element::from(text("Logging in")), jnovel::LoginState::LoggingIn => Element::from(text("Logging in")),
jnovel::LoginState::AwaitingConfirmation(code, start) => Element::from(text(format!("Login Code: {}. Expiring: {}s", code.otp, 600 - (Local::now() - start).num_seconds()))), jnovel::LoginState::AwaitingConfirmation(code, start) => Element::from(text(format!("Login Code: {}. Expiring: {}s", code.otp, 600 - (Local::now() - start).num_seconds()))),
jnovel::LoginState::Error(_) => Element::from(button("Error").on_press(Message::JncAction(jnovel::Message::LoginState(jnovel::LoginState::LoggedOut)))), jnovel::LoginState::Error(err) => Element::from(row![
button("Error").on_press(Message::JncAction(jnovel::Message::LoginState(jnovel::LoginState::LoggedOut))),
text(err),
]),
}; };
column(vec![Element::from( column(vec![Element::from(