From dc059b947d114c9f633cc59298020bfddca865e4 Mon Sep 17 00:00:00 2001 From: Mahesh Bansod Date: Fri, 19 Nov 2021 18:56:28 +0530 Subject: [PATCH] fix: create new session when the current expires --- cli/src/error.rs | 18 +++++++++++------- cli/src/main.rs | 44 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/cli/src/error.rs b/cli/src/error.rs index f9ce5f5..283d1a6 100644 --- a/cli/src/error.rs +++ b/cli/src/error.rs @@ -1,4 +1,5 @@ use socialvoid::{ClientError, SocialvoidError}; +use socialvoid_rawclient::AuthenticationError; use socialvoid_rawclient::ErrorKind; use socialvoid_rawclient::ValidationError; @@ -21,13 +22,16 @@ impl std::fmt::Display for MyFriendlyError { match &self.0 { SocialvoidError::RawClient(err) => match &err.kind { ErrorKind::Authentication(err) => { - write!(f, "This method needs you to log in. -Authentication Error: {:#?}\nIf you are already logged in, then try logging out and logging in again. -To log in: -socialvoid-cli login -To log out: -socialvoid-cli logout", err) - } + match err { + AuthenticationError::SessionExpired => write!(f, "The session was expired. Please login again."), + _ => write!(f, "This method needs you to log in. + Authentication Error: {:#?}\nIf you are already logged in, then try logging out and logging in again. + To log in: + socialvoid-cli login + To log out: + socialvoid-cli logout", err) + } + }, ErrorKind::Cdn(err) => { write!( f, diff --git a/cli/src/main.rs b/cli/src/main.rs index b8cad0c..853f2fe 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,4 +1,6 @@ use socialvoid::session::RegisterRequest; +use socialvoid_rawclient::AuthenticationError; +use socialvoid_rawclient::ErrorKind; use structopt::StructOpt; mod entities; @@ -33,18 +35,49 @@ async fn main() { }; let password = prompt_password("Enter password: "); //TODO: add OTP support - match sv.session.authenticate_user(username, password, None).await { - Err(err) => { - println!( + match sv + .session + .authenticate_user(username.clone(), password.clone(), None) + .await + { + Err(err) => match err.kind { + ErrorKind::Authentication(AuthenticationError::SessionExpired) => { + println!("Session expired. Creating new session and retrying."); + match sv.session.create().await { + Ok(_) => { + match sv + .session + .authenticate_user(username, password, None) + .await + { + Err(err) => println!( + "Couldn't authenticate the user.\n{}", + MyFriendlyError::from(err) + ), + Ok(_) => println!("Done."), + } + } + Err(err) => { + println!( + "Couldn't create the session.\n{}", + MyFriendlyError::from(err) + ) + } + } + } + _ => println!( "Couldn't authenticate the user.\n{}", MyFriendlyError::from(err) - ); - } + ), + }, Ok(_) => { println!("Successfully logged in."); } } } + SocialVoidCommand::Logout => { + let _ = sv.session.logout().await; + } SocialVoidCommand::Register => { if sv.session.authenticated() { panic!( @@ -253,6 +286,7 @@ enum SocialVoidCommand { Login { username: Option, }, + Logout, Register, Config { #[structopt(subcommand)]